Anyone know why this happens? Is this a bug of bash?
x='mnt:[4026532411]'
[[ $x == $x ]] && echo OK
I am expecting result OK
, but it did not.
Of course, this works
[[ "$x" == "$x" ]] && echo OK
But as I know, bash [[ ]] have a merit that no need to quote var when compare.
x='a b'
[[ $x == $x ]] && echo OK
works.
Ironical things is
x='mnt:[4026532411]'
[[ $x != $x ]] && echo Oh my god
result is Oh my god
Double Brackets i.e. [[]] is an enhanced (or extension) version of standard POSIX version, this is supported by bash and other shells(zsh,ksh). In bash, for numeric comparison we use eq , ne , lt and gt , with double brackets for comparison we can use == , !=
The square brackets are a synonym for the test command. An if statement checks the exit status of a command in order to decide which branch to take. grep -q "$text" is a command, but "$name" = 'Bob' is not--it's just an expression.
Double brackets in bash are not a command but a part of the language syntax. This means they can react more tolerantly to 'disappearing' arguments: $ [[ $a = $b ]] || echo "unequal" unequal.
The double bracket is a “compound command” where as test and the single bracket are shell built-ins (and in actuality are the same command). Thus, the single bracket and double bracket execute different code. The test and single bracket are the most portable as they exist as separate and external commands.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With