How to trim whitespace from a Bash variable?

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

    I found that I needed to add some code from a messy sdiff output in order to clean it up:

    sdiff -s column1.txt column2.txt | grep -F '<' | cut -f1 -d"<" > c12diff.txt 
    sed -n 1'p' c12diff.txt | sed 's/ *$//g' | tr -d '\n' | tr -d '\t'
    

    This removes the trailing spaces and other invisible characters.

提交回复
热议问题