Bird
0
0

Which of the following is the correct syntax to test if a string variable str is not empty?

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

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

    [ -n "$str" ] uses -n which correctly tests if str is not empty.
  3. Final Answer:

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

    Non-empty string test = -n [OK]
Quick Trick: Use -n to check non-empty strings safely [OK]
Common Mistakes:
MISTAKES
  • Using -z for not empty
  • Not quoting variable causing errors
  • Using [ $str ] which can fail if empty

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes