Bird
0
0

Which of the following is the correct way to write an if statement that checks if variable var equals 10 in bash?

easy📝 Syntax Q3 of 15
Bash Scripting - Conditionals
Which of the following is the correct way to write an if statement that checks if variable var equals 10 in bash?
Aif [ $var -eq 10 ]; then echo "Equal"; fi
Bif ( $var == 10 ) then echo "Equal" fi
Cif $var = 10 then echo "Equal" fi
Dif [ $var == 10 ] echo "Equal" fi
Step-by-Step Solution
Solution:
  1. Step 1: Understand bash if syntax

    The correct syntax uses square brackets with spaces and the '-eq' operator for numeric comparison.
  2. Step 2: Analyze options

    if [ $var -eq 10 ]; then echo "Equal"; fi correctly uses: if [ $var -eq 10 ]; then ... fi. Others misuse syntax or operators.
  3. Final Answer:

    if [ $var -eq 10 ]; then echo "Equal"; fi -> Option A
  4. Quick Check:

    Check for spaces and correct operator [OK]
Quick Trick: Use [ ] with spaces and -eq for numeric equality [OK]
Common Mistakes:
MISTAKES
  • Using parentheses instead of square brackets
  • Missing spaces inside [ ]
  • Using '=' instead of '-eq' for numbers
  • Omitting 'then' keyword

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes