How to trim whitespace from a Bash variable?

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

    With Bash's extended pattern matching features enabled (shopt -s extglob), you can use this:

    {trimmed##*( )}

    to remove an arbitrary amount of leading spaces.

提交回复
热议问题