0
0
Bash Scriptingscripting~10 mins

set -u for undefined variable errors in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to enable error on undefined variables.

Bash Scripting
set [1]
Drag options to blanks, or click blank then click option'
A-e
B-x
C-u
D-v
Attempts:
3 left
💡 Hint
Common Mistakes
Using -e instead of -u disables exit on error, not undefined variables.
Using -x enables debugging output, not error on undefined variables.
2fill in blank
medium

Complete the script to exit on undefined variable usage.

Bash Scripting
#!/bin/bash
set [1]
echo "$MY_VAR"
Drag options to blanks, or click blank then click option'
A-e
B-u
C-x
D-v
Attempts:
3 left
💡 Hint
Common Mistakes
Using -e only exits on command errors, not unset variables.
Forgetting to use set -u leads to silent empty variable expansion.
3fill in blank
hard

Fix the error by completing the code to enable undefined variable errors.

Bash Scripting
set [1]
if [ -z "$UNDEF_VAR" ]; then
  echo "Variable is empty"
fi
Drag options to blanks, or click blank then click option'
A-e
B-v
C-x
D-u
Attempts:
3 left
💡 Hint
Common Mistakes
Using -e does not catch unset variables.
Not using set -u allows silent empty variable usage.
4fill in blank
hard

Fill both blanks to enable error on undefined variables and print a message.

Bash Scripting
set [1]
echo "Value: $[2]"
Drag options to blanks, or click blank then click option'
A-u
BMY_VAR
CUNSET_VAR
D-e
Attempts:
3 left
💡 Hint
Common Mistakes
Using -e instead of -u for the option.
Using an undefined variable name causes script to exit.
5fill in blank
hard

Fill all three blanks to enable error on undefined variables, assign a value, and print it.

Bash Scripting
set [1]
[2]="Hello"
echo "Message: $[3]"
Drag options to blanks, or click blank then click option'
A-u
Bgreeting
D-e
Attempts:
3 left
💡 Hint
Common Mistakes
Using -e instead of -u for the option.
Using different variable names for assignment and print.