Some historical shells implemented a very simple parser that could get confused by things like
[ -n = "" ] where the first operand to
= looks like an operator, and would parse this as
[ -n = ] or cause a syntax error. In
[ "x$1" = x"" ], the
x prefix ensures that
x"$1" cannot possibly look like an operator, and so the only way the shell can parse this test is by treating
= as a binary operator.
All modern shells, and even most older shells still in operation, follow the POSIX rules which mandate that all test expressions of up to 4 words be parsed correctly. So
[ -z "$1" ] is a proper way of testing if
$1 is empty, and
[ "$x" = "$y" ] is a proper way to test the equality of two variables.
Even some current shells can get confused with longer expressions, and a few expressions are actually ambiguous, so avoid using the
-o operators to construct longer boolean tests, and instead use separate calls to
[ and the shell's own
|| boolean operators.