set -u
The shell option that makes using an unset variable an error that stops the script, instead of silently expanding to nothing.
In more detail
It catches mistyped names such as $nmae before they cause damage, for example an rm with an empty path. Use ${var:-} where a variable is allowed to be missing.
Related terms
- set -euo pipefail — The line many bash scripts put just under the shebang to turn on set -e, set -u and pipefail together.
- set -e — The shell option that makes a script exit as soon as a command fails, instead of carrying on.
- default value expansion — The ${name:-fallback} form, which gives the fallback when the variable is unset or empty, without changing the variable.
- unset variable — A variable that has never been assigned, or has been removed with the unset builtin, which is different from one set to an empty string.
Taught in
- set -e, set -u And pipefail: Promises And Traps — Bash Scripting & Command Line Automation
- Style That Prevents Bugs — Bash Scripting & Command Line Automation