bash [[ [a] == [a] ]] not true? square bracket affect compare result

后端 未结 2 695
夕颜
夕颜 2021-02-07 04:31

Anyone know why this happens? Is this a bug of bash?

x=\'mnt:[4026532411]\'

[[ $x == $x ]] && echo OK

I am expecting result OK

2条回答
  •  野的像风
    2021-02-07 04:37

    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
    

提交回复
热议问题