Bird
0
0

What is wrong with this bash snippet?

medium📝 Debug Q7 of 15
Bash Scripting - Conditionals
What is wrong with this bash snippet?
input=""
if [ -z input ]; then
  echo "Empty"
fi
AVariable input is not quoted inside -z test
BUsing -z instead of -n
CMissing else branch
DUsing single brackets instead of double brackets
Step-by-Step Solution
Solution:
  1. Step 1: Understand -z operator

    -z checks if a string is empty; it requires the variable to be expanded properly.
  2. Step 2: Importance of quoting variables

    Without quotes, if input is empty, test sees no argument, causing syntax errors.
  3. Final Answer:

    Variable input is not quoted inside -z test -> Option A
  4. Quick Check:

    Change to [ -z "$input" ] fixes the issue [OK]
Quick Trick: Always quote variables in tests like -z "$var" [OK]
Common Mistakes:
MISTAKES
  • Not quoting variables causing syntax errors
  • Confusing -z and -n usage
  • Assuming else branch is mandatory

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes