Saturday, 29 July 2017

string - How to trim whitespace from a Bash variable?

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.

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...