问题:
I have a shell script with this code: 我有一个带有以下代码的shell脚本:
var=`hg st -R "$path"`
if [ -n "$var" ]; then
echo $var
fi
But the conditional code always executes, because hg st
always prints at least one newline character. 但是条件代码总是执行,因为hg st
总是打印至少一个换行符。
- Is there a simple way to strip whitespace from
$var
(liketrim()
in PHP )? 有没有一种简单的方法可以从$var
删除空格(如PHP中的trim()
)?
or 要么
- Is there a standard way of dealing with this issue? 有没有解决此问题的标准方法?
I could use sed or AWK , but I'd like to think there is a more elegant solution to this problem. 我可以使用sed或AWK ,但是我想认为有一个更优雅的解决方案。
解决方案:
参考一: https://stackoom.com/question/1YBq/如何从Bash变量中修剪空格参考二: https://oldbug.net/q/1YBq/How-to-trim-whitespace-from-a-Bash-variable
来源:oschina
链接:https://my.oschina.net/u/4428122/blog/3275704