Bird
0
0

Why does ${#var} return 0 when var is unset in bash?

hard🧠 Conceptual Q10 of 15
Bash Scripting - String Operations
Why does ${#var} return 0 when var is unset in bash?
ABecause an unset variable is treated as an empty string
BBecause bash throws an error for unset variables
CBecause ${#var} counts words, not characters
DBecause variable length is undefined if unset
Step-by-Step Solution
Solution:
  1. Step 1: Understand unset variable behavior

    In bash, an unset variable is treated as an empty string when expanded.
  2. Step 2: ${#var} returns length of empty string as 0

    Therefore, length is zero, no error occurs.
  3. Final Answer:

    Because an unset variable is treated as an empty string -> Option A
  4. Quick Check:

    Unset var length = 0 [OK]
Quick Trick: Unset variables act like empty strings for length [OK]
Common Mistakes:
MISTAKES
  • Expecting error on unset variable length
  • Thinking length is undefined or null
  • Confusing unset with null or zero

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes