How to trim whitespace from a Bash variable?

后端 未结 30 2838
星月不相逢
星月不相逢 2020-11-22 06:09

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 ex

30条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 06:31

    I've always done it with sed

      var=`hg st -R "$path" | sed -e 's/  *$//'`
    

    If there is a more elegant solution, I hope somebody posts it.

提交回复
热议问题