Bash Shell: Trim Leading White Space From Input Variables

by on March 26, 2010 · 9 comments· LAST UPDATED March 26, 2010

in

My script depends upon $1 to take certain actions:

file="$1"
echo "|${file}|"

However, sometime there will leading while space which will result into:

|     output.txt|

How do I trim leading white space from one of my input variables?

There are many ways to remove leading white space from input variables. The simplest one use the read command as follows:

 
var="    output.txt"
echo "|$var|"
 

Sample outputs:

|    output.txt|

Use the read command to get rid of white space:

 
read  -rd '' var <<< "$var"
echo "|$var|"
 

Sample outputs:

|output.txt|

extglob

You can also use extglob shell option (Bash v2.2+) to turn on or off the extended pattern matching features.

 
var="         output.txt"
shopt -q -s extglob
echo "|${var##+([[:space:]])}|"
shopt -q -u extglob
 

You can store result to var itself:

var="${var##+([[:space:]])}"
echo "|${var}|"

The above syntax also works with ksh shell.

sed command

You can also use sed (see Here strings):

 
var="      output.txt"
var=$(sed -e 's/^[[:space:]]*//' <<<"$var")
echo "|${var}|"
 
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 9 comments… read them below or add one }

1 Philippe Petrinko March 26, 2010 at 11:02 am

Yeah right.
(and may be credits for this topic goes to Bash hackers page and could be referenced?)

Furthermore, if you want to get rid of all spaces,
you can use another bash function: search and replace brace expansion
that is :

${var// /}

or using as you suggest the “Space” class [[:space:]] to grab all kind of spaces.
(may be you should introduice a little character classes ?)


${var//[[:space:]]/}

As for instance:

v=" some thing here ";echo ":$v:";echo ":${v//[[:space:]]/}:"

Reply

2 Zdenek Styblik March 26, 2010 at 11:16 am

ead -rd '' var <<< "$var"
should be read

Reply

3 nixCraft March 26, 2010 at 12:05 pm

@ Zdenek:

Thanks for the heads up.

@Philippe: thanks for mentioning ${var// /} syntax.

Reply

4 Eric Shubert March 26, 2010 at 3:36 pm

How about
var=${var#*( )}
or, if you prefer the space class,
var=${var#*(:space:)}

I believe the search and replace syntax is more appropriately ${var//^ /} in order to replace only leading spaces, as the problem specified.

Reply

5 bashme March 28, 2010 at 2:32 am

Yall have forgotten the easiest by far …
t=" abc"; echo "|$t|" => | abc|
t=$(echo $t); echo "|$t|" => |abc|

Reply

6 Philippe Petrinko March 28, 2010 at 10:36 am

@Vivek – Fine then, if you appreciate ${var// /} syntax, would you add it to your article,
even if it is to get rid of all spaces, not just the leading ones.
This way, readers would come naturally to it, instead of having to browse all comments.

@Eric: I am afraid that neither of your proposal works.
Did you first test them?
If not, you should test before posting advice.
And if you did, you should post an entire self-explanatory example.

@bashme: No, because your proposal will trim leading _and_ trailing spaces, not just the leading ones. Check this:

v=" abc def ghi ";echo ":$v:";v=$(echo $v);echo ":${v}:"

Reply

7 Eric Shubert March 28, 2010 at 3:33 pm

@Philippe: – I did test both methods. Apparently not very effectively though. I can only guess that I dropped the leading | in my test echo at some point. Please accept my apology.

I was attempting to utilize a feature of the extglob shell option. This was probably a bad idea from the beginning because the option is usually off by default, and this is a feature of Pathname Expansion, which is not really related to the problem at hand.

Reply

8 Jalal Hajigholamali March 31, 2010 at 3:55 am

Hi,
Very useful topic

Reply

9 James Z April 2, 2010 at 9:46 am

Hi, the easist way is :)
v=`echo $v`

Reply

Leave a Comment

Tagged as: , , , , , , , , , ,

Previous Faq:

Next Faq: