Bird
0
0

Which of the following is the correct way to check if two strings stored in variables a and b are equal in bash?

easy📝 string Q3 of 15
Bash Scripting - Conditionals
Which of the following is the correct way to check if two strings stored in variables a and b are equal in bash?
A[ "$a" = "$b" ]
B[ $a == $b ]
C[ $a != $b ]
D[ -z "$a" ]
Step-by-Step Solution
Solution:
  1. Step 1: Understand string equality in bash

    Use single equals sign (=) inside test brackets to compare strings.
  2. Step 2: Importance of quoting variables

    Quoting variables prevents word splitting and errors if variables are empty or contain spaces.
  3. Final Answer:

    [ "$a" = "$b" ] -> Option A
  4. Quick Check:

    Test with a="hello" and b="hello" returns true [OK]
Quick Trick: Use [ "$a" = "$b" ] for string equality [OK]
Common Mistakes:
MISTAKES
  • Not quoting variables causing errors
  • Using == inside single brackets without [[ ]]
  • Using -z or -n for equality checks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes