Trimming Whitespace

Problem

You need to trim leading and/or trailing whitespace from lines for fields of data.

Solution

These solutions rely on a bash- specific treatment of read and $REPLY. See the end of the discussion for an alternate solution.

First, we’ll show a file with some leading and trailing whitespace. Note we add ~~ to show the whitespace. Note the → denotes a literal tab character in the output:

# Show the whitespace in our sample file
$ while read; do echo ~~"$REPLY"~~; done < whitespace
~~ This line has leading spaces.~~
~~This line has trailing spaces. ~~
~~ This line has both leading and trailing spaces. ~~
~~ → Leading tab.~~
~~Trailing tab. → ~~
~~ → Leading and trailing tab. → ~~
~~ → Leading mixed whitespace.~~
~~Trailing mixed whitespace. →     ~~
~~ → Leading and trailing mixed whitespace. →     ~~

To trim both leading and trailing whitespace use $IFS add the built-in REPLY variable (see the discussion for why this works):

$ while readREPLY; do echo ~~"$REPLY"~~; done < whitespace
~~This line has leading spaces.~~
~~This line has trailing spaces.~~
~~This line has both leading and trailing spaces.~~
~~Leading tab.~~
~~Trailing tab.~~
~~Leading and trailing tab.~~
~~Leading mixed whitespace.~~
~~Trailing mixed whitespace.~~
~~Leading and trailing mixed whitespace.~~

To trim only leading or only trailing spaces, use a simple pattern match:

# Leading spaces only
$ while read; do echo "~~${REPLY## }~~"; done < whitespace ~~This line has leading spaces.~~ ~~This line has trailing ...

Get bash Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.