Remove Whitespace

There is a few ways to remove white spaces in your documents/variables on Linux. Let's talk about three methods. The first one is done directly through bash, the second uses sed (my favorite method), and the third one uses the xargs command.


This is probably the worst way, so let's get it out of the way right away. With bash you can do the following and it will remove all the leading and tailing whitespaces in the variable var

var = "    abc    "
var = "${var#"${var%%[![:space:]]*}"}"      # remove leading
var = "${var%"${var##*[![:space:]]}"}"      # remove trailing

echo $var    
# displays "abc"


Wow that was something to type. I can hear you say "is there a faster and easier way to do this?". Yes, sed is here to help you.

sed 's/^[[:blank:]]*//g'    # replace leading blanks by empty string
sed 's/[[:blank:]]*$//g'    # replace training blanks by empty string

var = "    abc    "

echo $var | sed 's/^[[:space:]]*//g' | sed 's/[[:space:]]*$//g'
# displays "abc"


Okay, but what about multiple whitespaces that are not at the begining nor the end. Is there a wway to remove those? xargs might be able to help you. Although it's quite powerful, it might not be the command you are looking for. xargs will not only remove the spaces, but MAY also remove double and single quotes. See the following example for more detail.

var = "a    b"
xargs $var           # "a b"

var = "a\"b\"c'd'e"
xargs $var           # "abcde"

var = "a\"b"
xargs $var           # "a\"b"

Also note that xargs will add an endline (\n) at the end of each row. You may not want those endlines depending on your needs.


In the current tutorial we use [[:space:]] and [[:blank:]]. You will be able to find the definition for those and many more in the following box

 [[:alnum:]]  - [A-Za-z0-9]     Alphanumeric characters
 [[:alpha:]]  - [A-Za-z]        Alphabetic characters
 [[:blank:]]  - [ \x09]         Space or tab characters only
 [[:cntrl:]]  - [\x00-\x19\x7F] Control characters
 [[:digit:]]  - [0-9]           Numeric characters
 [[:graph:]]  - [!-~]           Printable and visible characters
 [[:lower:]]  - [a-z]           Lower-case alphabetic characters
 [[:print:]]  - [ -~]           Printable (non-Control) characters
 [[:punct:]]  - [!-/:-@[-`{-~]  Punctuation characters
 [[:space:]]  - [ \t\v\f]       All whitespace chars
 [[:upper:]]  - [A-Z]           Upper-case alphabetic characters
 [[:xdigit:]] - [0-9a-fA-F]     Hexadecimal digit characters

This table was writen by slm at how do I trim leading and trailing whitespace from each line of some output?, consulted by Cajetan Bouchard on september 25th, 2016.