set -u (Undefined Variables) and set -x (Trace Mode)
In this page:
Catching Unset Variables
With set -u active, referencing a variable that was never assigned causes an immediate error and non-zero exit, which helps catch typos in variable names early instead of them silently expanding to nothing.
Example: Catching Unset Variables
#!/bin/bash
(
set -u
echo "$defined_var" 2>/dev/null
) ; defined_var="hello"
(
set -u
echo "now it's set: $defined_var"
)
Login to try C/C++/Java/PHP code in the editor
Safely Demonstrating set -u's Error
Running the failing case inside a subshell and checking its overall result with || lets a script show what set -u catches without letting that failure end the whole script.
Example: Safely Demonstrating set -u's Error
#!/bin/bash
(set -u; echo "$totally_undefined_variable") 2>/dev/null || echo "caught: set -u stopped an unset variable reference"
Login to try C/C++/Java/PHP code in the editor
Providing Defaults to Avoid set -u Errors
${var:-default} expands to default whenever var is unset (or empty), which is the standard way to make a script compatible with set -u while still allowing optional variables, like positional parameters that may not be given.
Example: Providing Defaults to Avoid set -u Errors
#!/bin/bash
set -u
name="${1:-anonymous}"
echo "Hello, $name"
Login to try C/C++/Java/PHP code in the editor
Tracing Execution with set -x
set -x makes bash print each command to stderr, prefixed with +, right before running it, with variables already expanded to their actual values. This is one of the fastest ways to see exactly what a script is doing.
Note: Redirect the trace output separately with exec 2>trace.log if you want to keep it out of the normal terminal output.
Warning: set -x can print sensitive values like passwords or tokens; avoid it around code that handles secrets.
Example: Tracing Execution with set -x
#!/bin/bash
set -x
num=5
double=$((num * 2))
set +x
echo "tracing disabled again; double is $double"
Login to try C/C++/Java/PHP code in the editor
- Forgetting
set -utreats an unset variable as an error even in something as common as$1when no argument was passed, which needs a default like${1:-}. - Leaving
set -xenabled in production output, which can leak sensitive values (like passwords stored in variables) into logs. - Assuming
set -ucatches a variable that is set to an empty string; empty and unset are different, andset -uonly complains about the latter.
set -ucauses an error when an unset variable is referenced, instead of silently treating it as an empty string.${var:-default}provides a fallback value and avoids aset -uerror for a variable that might not be set.set -xprints each command (with variables expanded) to stderr right before it executes, which is invaluable for debugging.set +uandset +xturn each mode back off; both can also be combined withset -eas part of a strict-mode header.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Exit Codes and Checking $? Correctly
- set -e and set -u
- Error Handling
- set -e (Exit on Error) and Its Gotchas
- Logging
- set -u (Undefined Variables) and set -x (Trace Mode)
- Script Arguments with getopts
- trap ERR for Custom Error Handling
- Debug Mode (set -x)
- Writing a Custom Error/die Function
- Portability
- Validating Script Input and Arguments