Bash Trim Leading and Trailing Whitespace from a String

Posted on November 3, 2020 at 11:56 pm

You can use awk to trim a string, example:

echo "    test    " | awk '{$1=$1};1'

Another way is to use sed like this:

echo "    test    " | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'

And also xargs can be used:

echo "    test    " | xargs

Receive updates via email

Other Posts

Updated Posts