Bird
0
0

Consider this script with set -u enabled:

medium📝 Debug Q6 of 15
Bash Scripting - Error Handling
Consider this script with set -u enabled:
#!/bin/bash
set -u

if [ "$user" = "admin" ]; then
  echo "Welcome admin"
fi

What error occurs and how can it be resolved?
AError due to unset variable 'user'; fix by initializing 'user' before use
BSyntax error in if statement; fix by adding spaces around '='
CNo error; script runs fine with set -u
DError due to missing shebang; fix by adding '#!/bin/bash'
Step-by-Step Solution
Solution:
  1. Step 1: Identify the error

    With set -u, referencing an unset variable like user causes an error.
  2. Step 2: Fix the error

    Initialize user before the if statement, e.g., user="admin" or check if set.
  3. Final Answer:

    Error due to unset variable 'user'; fix by initializing 'user' before use -> Option A
  4. Quick Check:

    Unset variables cause errors with set -u [OK]
Quick Trick: Initialize variables before use with set -u enabled [OK]
Common Mistakes:
MISTAKES
  • Assuming variables are auto-initialized
  • Ignoring set -u errors and continuing
  • Misinterpreting syntax errors as variable errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes