Bash Shell: Trim Leading White Space From Input Variables

last updated in Categories

My script depends upon $1 to take certain actions:

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:



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:

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}|"


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

9 comment

  1. 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 ?)


    As for instance:

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

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

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

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

  4. @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}:"

  5. @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.

    Still, have a question? Get help on our forum!