How to trim whitespace from a Bash variable?

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

    You can delete newlines with tr:

    var=`hg st -R "$path" | tr -d '\n'`
    if [ -n $var ]; then
        echo $var
    done
    

提交回复
热议问题