Bird
0
0

Which of the following is the correct syntax to check if variable str is empty using [[ ]]?

easy📝 Syntax Q3 of 15
Bash Scripting - Conditionals
Which of the following is the correct syntax to check if variable str is empty using [[ ]]?
A[[ $str != "" ]]
B[[ -z $str ]]
C[[ -n $str ]]
D[[ $str == * ]]
Step-by-Step Solution
Solution:
  1. Step 1: Understand string length tests in [[ ]]

    -z tests if string length is zero (empty), -n tests if not empty.
  2. Step 2: Choose correct test for empty string

    [[ -z $str ]] returns true if str is empty, which is the correct way.
  3. Final Answer:

    [[ -z $str ]] -> Option B
  4. Quick Check:

    Empty string test = D [OK]
Quick Trick: Use [[ -z $var ]] to check if variable is empty [OK]
Common Mistakes:
MISTAKES
  • Using == "" which is less reliable
  • Confusing -z and -n
  • Forgetting to quote variables in some contexts

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes