Why does Bash treat undefined variables as true in an ‘if’ statement?

The argument to if is a statement to execute, and its exit status is tested. If the exit status is 0 the condition is true and the statements in then are executed.

When $CONDITION isn’t set, the statement is an empty statement, and empty statements always have a zero exit status, meaning success. So the if condition succeeds.

This behavior is explained in the POSIX shell specification:

If there is no command name, but the command contained a command substitution, the command shall complete with the exit status of the last command substitution performed. Otherwise, the command shall complete with a zero exit status.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)