[Bash] Treat unset variables as errors with set -u

Created on November 30, 2026

Enable set -u to make Bash report an error if a script expands an unset variable. The example suppresses the error text and prints the child shell's exit status.

bash -c 'set -u; echo "$UNDEFINED"' 2>/dev/null
echo $?
127

set -u is also called set -o nounset. In a script, expanding the unset variable causes the shell to exit.

Bash