An alternate I've seen to [ -z "$foo" ]
is the following, however I'm not sure why people use this method, anyone know?
[ "x${foo}" = "x" ]
Anyway if you're disallowing unset variables (either by set -u
or set -o nounset
), then you'll run into trouble with both of those methods. There's a simple fix to this:
[ -z "${foo:-}" ]
Note: this will leave your variable undef.