Challenge - 5 Problems
Set -u Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output when using
set -u with an undefined variable?Consider this bash script snippet with
set -u enabled. What will be the output or error?Bash Scripting
set -u echo "Value: $UNDEFINED_VAR"
Attempts:
2 left
💡 Hint
Think about what
set -u does when a variable is not set.✗ Incorrect
The set -u option causes the shell to treat any attempt to use an undefined variable as an error. So, referencing $UNDEFINED_VAR will cause the script to stop and print an error message.
💻 Command Output
intermediate2:00remaining
How to safely use an undefined variable with
set -u?Given
set -u is enabled, which command safely prints the value of MY_VAR without error if it is undefined?Bash Scripting
set -u # Which echo command below works safely?
Attempts:
2 left
💡 Hint
Look for the syntax that provides a default value if the variable is unset.
✗ Incorrect
The ${VAR:-default} syntax returns default if VAR is unset or null, preventing errors with set -u.
🔧 Debug
advanced2:00remaining
Why does this script fail with
set -u enabled?Examine the script below. Why does it fail when
set -u is set?Bash Scripting
set -u function greet() { echo "Hello, $1!" } greet $NAME
Attempts:
2 left
💡 Hint
Check which variable is undefined and how
set -u treats it.✗ Incorrect
The variable $NAME is not defined before calling greet $NAME. With set -u, referencing an undefined variable causes an error.
🚀 Application
advanced2:00remaining
Modify this script to avoid errors with
set -uGiven the script below, which modification prevents errors when
set -u is enabled and CONFIG_PATH might be unset?Bash Scripting
set -u if [ -f "$CONFIG_PATH" ]; then echo "Config found" else echo "Config missing" fi
Attempts:
2 left
💡 Hint
Use parameter expansion to provide a safe default for unset variables.
✗ Incorrect
The ${VAR:-} syntax expands to an empty string if VAR is unset, preventing set -u errors.
🧠 Conceptual
expert2:00remaining
What is the main purpose of
set -u in bash scripting?Choose the best description of what
set -u does in a bash script.Attempts:
2 left
💡 Hint
Think about how
set -u helps catch mistakes with variables.✗ Incorrect
set -u makes the shell exit with an error if you try to use a variable that has not been set. This helps catch bugs early.