4 ms·
> echo "$FOO" | sed -e ' s/^[[:space:]]//g; s/[[:space:]]$//g ' Your example fails when $FOO is "-n", for instance. Also the g modifiers are redundant in your
by jolmg 7y ago
> echo "$FOO" | sed -e ' s/^[[:space:]]//g; s/[[:space:]]$//g '
Your example fails when $FOO is "-n", for instance. Also the g modifiers are redundant in your example since there's only one beginning of line per line and one end of line per line.
I would instead write this:
sed -r 's/^\s+|\s+$//g' <<< "$FOO"
EDIT: I think I see now what you probably thought would happen by using g, but no it wouldn't remove multiple spaces. So, you example also fails when $FOO is " x" (using 2 or more spaces at the ends).
- jwilk 7y agoBeware that -r is a GNU-specific option.