设置后:
var1="a\nb"
var2="$var1"
我希望 $var1
和 $var2
相等;然而:
[[ $var1 == $var2 ]] && echo yes || echo no
返回没有
。这是为什么?
最佳答案
您必须引用才能执行字符串比较:
[[ "$var1" == "$var2" ]]
所以这是可行的:
$ var1="a\nb"
$ var2="$var1"
$ [[ "$var1" == "$var2" ]] && echo yes || echo no
yes
为什么会这样?
从评论中,chepner indicates :
Without the quotes, $var2 is interpreted as a pattern, not a string, and the \n is treated as a regular n.
关于bash - 为什么包含\n 的 bash 变量不等于它的副本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26782601/