Hi.
I'm new to this forum, my English perhaps is not so good, but here is my question:
In bash you can use [[ ]] for tests, and how I understand it the variable names should be expanded automatically. So this should give "yes":
xx=hello
$ [[ xx == hello ]] && echo yes || echo no
no # not giving "yes"
These two are however working:
$ [[ $xx == hello ]] && echo yes || echo no
yes # correct
# and
nn=77
$ [[ nn -eq 77 ]] && echo yes || echo no
yes # correct
Why isn't the variable expansion working for strings? Or I missed something?
The expression that is contained within double braces can take many forms, but the general syntax is one of
<operator> <string>
or
<operator> <filename>
or
<string> <operator> <pattern>
Because the strings and filenames do not need to be quoted, there is no way for the interpreter to determine whether xx is a string or a variable name that should be expanded. Thus, you must explicitly dereference the variables you wish to have expanded as you have done in your first working example.
This is different than in arithmetic expressions (( expression )) where the interpreter can assume that any string references are variable names to be evaluated as such.