How to trim whitespace from bash variable?

Posted by too much php on Stack Overflow See other posts from Stack Overflow or by too much php
Published on 2008-12-15T21:24:01Z Indexed on 2010/06/13 10:12 UTC
Read the original article Hit count: 337

Filed under:
|
|

I have a shell script with this code:

var=`hg st -R "$path"`
if [ -n "$var" ]; then
    echo $var
fi

But the conditional code always executes because hg st always prints at least one newline character.

  • Is there a simple way to strip whitespace from $var (like trim() in php)?

or

  • Is there a standard way of dealing with this issue?

I could use sed or awk, but I'd like to think there is a more elegant solution to this problem.

© Stack Overflow or respective owner

Related posts about string

Related posts about bash