Bird
0
0

Which of the following is the correct syntax to test if variable var is equal to string hello in bash?

easy📝 Syntax Q12 of 15
Bash Scripting - Conditionals
Which of the following is the correct syntax to test if variable var is equal to string hello in bash?
A[ $var = hello ]
B[ $var == hello ]
C[ "$var" = "hello" ]
D[ "$var" == hello ]
Step-by-Step Solution
Solution:
  1. Step 1: Check quoting rules

    Variables should be quoted to avoid errors if empty or contain spaces, so "$var" is correct.
  2. Step 2: Check operator correctness

    Single equals = is POSIX compliant for string comparison inside [ ]. Double equals == works in [[ ]] but not always in [ ].
  3. Final Answer:

    [ "$var" = "hello" ] -> Option C
  4. Quick Check:

    Quote variables and use = inside [ ] [OK]
Quick Trick: Always quote variables and use = inside [ ] [OK]
Common Mistakes:
MISTAKES
  • Not quoting variables causing syntax errors
  • Using == inside [ ] which may fail
  • Missing spaces around brackets

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes