How to trim whitespace from a Bash variable? How to trim whitespace from a Bash variable? bash bash

How to trim whitespace from a Bash variable?


A simple answer is:

echo "   lol  " | xargs

Xargs will do the trimming for you. It's one command/program, no parameters, returns the trimmed string, easy as that!

Note: this doesn't remove all internal spaces so "foo bar" stays the same; it does NOT become "foobar". However, multiple spaces will be condensed to single spaces, so "foo bar" will become "foo bar". In addition it doesn't remove end of lines characters.


Let's define a variable containing leading, trailing, and intermediate whitespace:

FOO=' test test test 'echo -e "FOO='${FOO}'"# > FOO=' test test test 'echo -e "length(FOO)==${#FOO}"# > length(FOO)==16

How to remove all whitespace (denoted by [:space:] in tr):

FOO=' test test test 'FOO_NO_WHITESPACE="$(echo -e "${FOO}" | tr -d '[:space:]')"echo -e "FOO_NO_WHITESPACE='${FOO_NO_WHITESPACE}'"# > FOO_NO_WHITESPACE='testtesttest'echo -e "length(FOO_NO_WHITESPACE)==${#FOO_NO_WHITESPACE}"# > length(FOO_NO_WHITESPACE)==12

How to remove leading whitespace only:

FOO=' test test test 'FOO_NO_LEAD_SPACE="$(echo -e "${FOO}" | sed -e 's/^[[:space:]]*//')"echo -e "FOO_NO_LEAD_SPACE='${FOO_NO_LEAD_SPACE}'"# > FOO_NO_LEAD_SPACE='test test test 'echo -e "length(FOO_NO_LEAD_SPACE)==${#FOO_NO_LEAD_SPACE}"# > length(FOO_NO_LEAD_SPACE)==15

How to remove trailing whitespace only:

FOO=' test test test 'FOO_NO_TRAIL_SPACE="$(echo -e "${FOO}" | sed -e 's/[[:space:]]*$//')"echo -e "FOO_NO_TRAIL_SPACE='${FOO_NO_TRAIL_SPACE}'"# > FOO_NO_TRAIL_SPACE=' test test test'echo -e "length(FOO_NO_TRAIL_SPACE)==${#FOO_NO_TRAIL_SPACE}"# > length(FOO_NO_TRAIL_SPACE)==15

How to remove both leading and trailing spaces--chain the seds:

FOO=' test test test 'FOO_NO_EXTERNAL_SPACE="$(echo -e "${FOO}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"echo -e "FOO_NO_EXTERNAL_SPACE='${FOO_NO_EXTERNAL_SPACE}'"# > FOO_NO_EXTERNAL_SPACE='test test test'echo -e "length(FOO_NO_EXTERNAL_SPACE)==${#FOO_NO_EXTERNAL_SPACE}"# > length(FOO_NO_EXTERNAL_SPACE)==14

Alternatively, if your bash supports it, you can replace echo -e "${FOO}" | sed ... with sed ... <<<${FOO}, like so (for trailing whitespace):

FOO_NO_TRAIL_SPACE="$(sed -e 's/[[:space:]]*$//' <<<${FOO})"


There is a solution which only uses Bash built-ins called wildcards:

var="    abc    "# remove leading whitespace charactersvar="${var#"${var%%[![:space:]]*}"}"# remove trailing whitespace charactersvar="${var%"${var##*[![:space:]]}"}"   printf '%s' "===$var==="

Here's the same wrapped in a function:

trim() {    local var="$*"    # remove leading whitespace characters    var="${var#"${var%%[![:space:]]*}"}"    # remove trailing whitespace characters    var="${var%"${var##*[![:space:]]}"}"       printf '%s' "$var"}

You pass the string to be trimmed in quoted form. e.g.:

trim "   abc   "

A nice thing about this solution is that it will work with any POSIX-compliant shell.

Reference