Isn't this one of the things that bash [[ ]] explicitly fixes?
% bash -c 'a=xy; b="x*"; [[ $a == $b ]] && echo "$a == $b"'
xy == x*
My hot take is that you should avoid using [[, preferring to use [, which at least pretends to work like a normal program that can be invoked using normal quoting rules—it just interprets a funky mini-language on its command-line arguments. [[ is some exotic thing that seems reasonable at first glance but follows no rules but its own and varies its behavior depending on syntactic nits that literally cannot possibly matter and aren't even distinguishable for normal programs.And then just quote consistently, which is something you need know how to do with almost every other line of a shell script anyway!
I tend to agree, and have taken this one step further, preferring the "test" command:
if test "$a" = "foo" ; then
I do this for a few reasons:- "Subsetting" your language can be beneficial: https://wiki.c2.com/?LanguageSubset
- "test" reinforces the notion that this is just the name of a program, not a syntax construct, which encourages using other programs as predicates, e.g. 'if which -s foo'.
- "man test" doesn't force you to wade through a 5,000 line man page.
Recent example: https://leopard.sh/leopard.sh
So [[ $a == "$b" ]] works.
$ shellcheck oops.sh
In oops.sh line 4:
[[ $a == $b ]]
^-- SC2053 (warning): Quote the right-hand side of == in [[ ]] to prevent glob matching.
For more information:
https://www.shellcheck.net/wiki/SC2053 -- Quote the right-hand side of == i...
Unlike the bugs that the x$var works around this is all in the manual!https://www.gnu.org/software/bash/manual/bash.html#index-_00...
[[ $var == "" ]]
works as you describe and as expected. What the parent is talking about is when you have an invocation like my_program $opt
my_program "$opt"
Which when empty will evaluate to my_program
my_program ""
respectively.