How to trim whitespace from a Bash variable?

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

    You can trim simply with echo:

    foo=" qsdqsd qsdqs q qs   "
    
    # Not trimmed
    echo \'$foo\'
    
    # Trim
    foo=`echo $foo`
    
    # Trimmed
    echo \'$foo\'
    

提交回复
热议问题