问题描述
有人知道为什么会这样吗?这是bash的错误吗?
Anyone know why this happens? Is this a bug of bash?
x='mnt:[4026532411]'
[[ $x == $x ]] && echo OK
我期望结果 OK
,但是没有.
I am expecting result OK
, but it did not.
当然可以,
[[ "$x" == "$x" ]] && echo OK
但是据我所知,bash [[]]的优点是比较时无需引用var.
But as I know, bash [[ ]] have a merit that no need to quote var when compare.
x='a b'
[[ $x == $x ]] && echo OK
有效.
讽刺的事情是
x='mnt:[4026532411]'
[[ $x != $x ]] && echo Oh my god
结果是我的天哪
推荐答案
未引用的 ==
和!=
的右侧被视为一种模式,而不是文字字符串. mnt:[4026532411]
将与 mnt:
匹配,后跟精确的 one 分别为0、1、2、3、4、5或6,因为模式 mnt:[4026532411]
和 mnt:[0123456]
是等效的.要匹配字符串,您需要引用扩展名.
The unquoted right-hand side of ==
and !=
is treated as a pattern, not a literal string. mnt:[4026532411]
will match mnt:
followed by exactly one of 0, 1, 2, 3, 4, 5, or 6, since the patterns mnt:[4026532411]
and mnt:[0123456]
are equivalent. To match the lieral string, you need to quote the expansion.
x='mnt:[4026532411]'
[[ $x == "$x" ]] && echo OK
这篇关于bash [[[a] == [a]]]不正确吗?方括号影响比较结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!