Bird
0
0

Why does set -u cause a script to fail when using variables inside double quotes if they are unset, and how can this behavior improve script reliability?

hard🧠 Conceptual Q10 of 15
Bash Scripting - Error Handling
Why does set -u cause a script to fail when using variables inside double quotes if they are unset, and how can this behavior improve script reliability?
ABecause set -u disables all variable usage; it makes scripts shorter
BBecause referencing unset variables causes errors; it prevents silent bugs by forcing explicit initialization
CBecause double quotes disable variable expansion; it forces use of single quotes
DBecause set -u only works outside quotes; it encourages avoiding quotes
Step-by-Step Solution
Solution:
  1. Step 1: Understand set -u behavior with unset variables

    set -u causes an error when an unset variable is referenced, even inside double quotes.

  2. Step 2: Explain reliability benefit

    This behavior forces the script author to initialize variables explicitly, preventing silent bugs from typos or missing values.

  3. Final Answer:

    Because referencing unset variables causes errors; it prevents silent bugs by forcing explicit initialization -> Option B
  4. Quick Check:

    set -u enforces variable initialization for reliability [OK]
Quick Trick: set -u forces explicit variable initialization to avoid bugs [OK]
Common Mistakes:
MISTAKES
  • Thinking quotes disable variable expansion
  • Believing set -u disables all variables
  • Assuming set -u only works outside quotes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes