Bird
0
0

What will be the output of this script?

medium📝 Command Output Q5 of 15
Bash Scripting - Conditionals
What will be the output of this script?
#!/bin/bash
num=0
if [ $num ]; then
  echo "True"
else
  echo "False"
fi
AFalse
BTrue
CSyntax error
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand condition evaluation

    In bash, [ $num ] tests if $num is non-empty string. "0" is non-empty, so condition is true.
  2. Step 2: Determine output

    Since condition is true, it prints "True".
  3. Final Answer:

    True -> Option B
  4. Quick Check:

    Non-empty string condition = true [OK]
Quick Trick: Non-empty strings are true in [ ] tests [OK]
Common Mistakes:
MISTAKES
  • Assuming 0 is false
  • Missing spaces in [ ]
  • Confusing with numeric tests

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes