In POSIX-compliant shells,
test "$foo" = "$bar"
does a string comparison no matter what the contents of the variables foo
and bar
are. But in old, noncompliant shells, if foo
contained something that begins with a dash, test
would try to interpret that as a unary operator, so it would either throw a syntax error or do the wrong test. Writing x"$foo" = x"$bar"
makes that impossible. I also vaguely recall problems if either argument expanded to the empty string, but that might have only been if you left out the double quotes (in which case it would be a problem with a modern shell, too).
This is particularly relevant when writing configure scripts, because, at least in principle, they're supposed to work no matter how old the shell environment is; and if you're writing a new program today, in 2020, the most plausible reason why you'd use C and autoconf is that you need portability to some old systems...
test "" = "foo"
– Dap