Taken this snippet:
$ [[ ""=="foo" ]] && echo yes || echo no
[[ -n ==foo ]]
echo yes
yes
How does [[ ""=="foo" ]]
turn into [[ -n ==foo ]]
?
The RC was of course missing spaces around ==
- after adding them, it works as expected:
$ [[ "" == "foo" ]] && echo yes || echo no
[[ '' == \f\o\o ]]
echo no
no
But still i cannot understand why it behaved like this?
CodePudding user response:
It's not changing the empty string into -n
.
The string ""=="foo"
is equivalent to the string ==foo
. The trace output always shows strings in their simplest format, without unnecessary quotes.
A conditional expression that just contains a single string with no operators is true if the string is not empty. That's what the -n
operator tests, so the -x
expansion shows it that way.
CodePudding user response:
Any operand that isn't preceded or followed by an operator is treated to have an equal operation as -n <operand>
. Operators also need to be isolated with spaces to be distinguished. For a list of operators run help test
. Also run help [[
to see how the keyword is different from the [
and test
builtins.