Consider a simple debugging style where $debug would be set either to true or false according to some command-line flag:
$debug && echo "Something strange just happened" >&2
I know there are better ways to protect against value setting, such as ${debug:-false} or even a full-featured logMessage()-type function, but let's park that for now.
Assume the bad situation where $debug remains unset or empty. I expected that $debug && echo … for such an empty $debug would be parsed and evaluated into && echo …, which would trigger a consequent syntax error. But instead it seems to be evaluated into something like : && echo …, which ends up executing the echo.
I've tested with bash, ksh, and dash and the scenario remains consistent.
My understanding was that these two lines parsed out as equivalent, but clearly they do not:
unset debug; $debug && echo stuff
unset debug; && echo stuff
Why does the code act as if an unset variable is logically true instead of failing with an error such as syntax error near unexpected token `&&'?