baumi's blog

baumi's personal blog … Linux, OS X, Windows, Random things, …

Bash: trim left and right (leading and trailing whitespaces)

Wanted: remove leading and trailing whitespaces from variables or cat’d lines from a textfile. – Simple solution:

echo " a b c d " | awk '{$1=$1};1'

prints:

"a b c d"

For convenience, i created /usr/bin/trim with this content:

#!/bin/bash
awk '{$1=$1};1'

which then allows

echo " a b c d " | trim

Comments are currently closed.