You don't need the x-hack for strings. In shell, a widely-recommended form for ANY expansion of a variable uses quoting unless you have special requirements. E.g., normally don't use $foo, use "$foo". Tools like shellcheck enforce this. Not ideal, but not a big deal either.
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!
- "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.
Well techincally... the == and != operator matches the LHS against the pattern on the RHS.
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!
Empty strings had always been the case that I'd been taught required the x-hack.