Bird
0
0

Find the error in this script:

medium📝 Debug Q6 of 15
Bash Scripting - Conditionals
Find the error in this script:
#!/bin/bash
if [ $num -eq 5 ]then
  echo "Number is 5"
fi
AWrong operator -eq should be ==
BMissing fi to close if
CMissing semicolon or newline before then
DVariable $num not declared
Step-by-Step Solution
Solution:
  1. Step 1: Check syntax of if statement

    Bash requires a semicolon before then if on the same line or a newline before then. Here, then immediately follows ] without ; or newline, causing syntax error.
  2. Step 2: Identify the error

    Missing semicolon or newline before then correctly identifies the syntax error; other options do not: wrong operator suggestion, missing fi (present), undeclared variable (runtime issue, not syntax).
  3. Final Answer:

    Missing semicolon or newline before then -> Option C
  4. Quick Check:

    Then must follow semicolon or newline = B [OK]
Quick Trick: Put semicolon or newline before then [OK]
Common Mistakes:
MISTAKES
  • Using == for numeric test
  • Forgetting fi
  • Ignoring variable declaration

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes