How to trim whitespace from a Bash variable?

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

    This will remove all the whitespaces from your String,

     VAR2="${VAR2//[[:space:]]/}"
    

    / replaces the first occurrence and // all occurrences of whitespaces in the string. I.e. all white spaces get replaced by – nothing

提交回复
热议问题