How to trim whitespace from a Bash variable?

后端 未结 30 2894
星月不相逢
星月不相逢 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:39

    Assignments ignore leading and trailing whitespace and as such can be used to trim:

    $ var=`echo '   hello'`; echo $var
    hello
    

提交回复
热议问题