O'Reilly logo

bash Cookbook by Cameron Newham, JP Vossen, Carl Albing

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required