Bird
0
0

What will be the output of this script?

medium📝 Command Output Q13 of 15
Bash Scripting - Conditionals
What will be the output of this script?
#!/bin/bash
num=7
if [ $num -lt 5 ]; then
  echo "Less than 5"
elif [ $num -eq 7 ]; then
  echo "Equal to 7"
else
  echo "Other"
fi
ALess than 5
BEqual to 7
COther
DSyntax Error
Step-by-Step Solution
Solution:
  1. Step 1: Check the value of num and conditions

    num=7. First condition checks if num < 5 (false). Second checks if num == 7 (true).
  2. Step 2: Determine which block runs

    Since second condition is true, it prints "Equal to 7" and skips else.
  3. Final Answer:

    Equal to 7 -> Option B
  4. Quick Check:

    num=7 matches elif condition [OK]
Quick Trick: Check conditions top to bottom, first true runs [OK]
Common Mistakes:
MISTAKES
  • Assuming first condition runs
  • Confusing -lt and -eq operators
  • Missing fi causes syntax error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes