Bird
0
0

You want to write a bash script that prints "Empty string" if variable text is empty, and "Has content" otherwise. Which snippet correctly does this?

hard🚀 Application Q15 of 15
Bash Scripting - Conditionals
You want to write a bash script that prints "Empty string" if variable text is empty, and "Has content" otherwise. Which snippet correctly does this?
Aif [ "$text" != "" ]; then echo "Empty string"; else echo "Has content"; fi
Bif [[ "$text" = "" ]]; then echo "Has content"; else echo "Empty string"; fi
Cif [ -n "$text" ]; then echo "Empty string"; else echo "Has content"; fi
Dif [[ -z "$text" ]]; then echo "Empty string"; else echo "Has content"; fi
Step-by-Step Solution
Solution:
  1. Step 1: Understand -z usage

    -z "$text" returns true if text is empty.
  2. Step 2: Check echo outputs match condition

    If empty, print "Empty string"; else print "Has content". if [[ -z "$text" ]]; then echo "Empty string"; else echo "Has content"; fi matches this logic correctly.
  3. Final Answer:

    if [[ -z "$text" ]]; then echo "Empty string"; else echo "Has content"; fi -> Option D
  4. Quick Check:

    -z empty -> print "Empty string" [OK]
Quick Trick: Use if [[ -z "$var" ]] for empty string check [OK]
Common Mistakes:
MISTAKES
  • Swapping outputs for empty and non-empty cases
  • Using = "" without quotes or wrong logic
  • Confusing -n and -z meanings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes