Bird
Raised Fist0
Bash Scriptingscripting~20 mins

String comparisons (=, !=, -z, -n) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
String Comparison Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Bash script snippet?
Consider the following Bash code:
str="hello"
if [ "$str" = "hello" ]; then
  echo "Match"
else
  echo "No match"
fi
What will be printed when this script runs?
Bash Scripting
str="hello"
if [ "$str" = "hello" ]; then
  echo "Match"
else
  echo "No match"
fi
ASyntax error
BNo match
CMatch
DEmpty output
Attempts:
2 left
💡 Hint
Check how string equality is tested in Bash with single brackets.
💻 Command Output
intermediate
2:00remaining
What does this Bash condition check?
What is the output of this Bash snippet?
str=""
if [ -z "$str" ]; then
  echo "Empty"
else
  echo "Not empty"
fi
Bash Scripting
str=""
if [ -z "$str" ]; then
  echo "Empty"
else
  echo "Not empty"
fi
ANot empty
BEmpty
CSyntax error
DNo output
Attempts:
2 left
💡 Hint
The -z test checks if a string is empty.
💻 Command Output
advanced
2:00remaining
What is the output of this Bash script with string inequality?
Analyze this Bash code:
str="world"
if [ "$str" != "hello" ]; then
  echo "Different"
else
  echo "Same"
fi
What will it print?
Bash Scripting
str="world"
if [ "$str" != "hello" ]; then
  echo "Different"
else
  echo "Same"
fi
ADifferent
BSame
CSyntax error
DNo output
Attempts:
2 left
💡 Hint
The != operator checks if strings are not equal.
💻 Command Output
advanced
2:00remaining
What happens when using -n with a non-empty string?
What is the output of this Bash snippet?
str="text"
if [ -n "$str" ]; then
  echo "Has content"
else
  echo "Empty"
fi
Bash Scripting
str="text"
if [ -n "$str" ]; then
  echo "Has content"
else
  echo "Empty"
fi
ASyntax error
BEmpty
CNo output
DHas content
Attempts:
2 left
💡 Hint
The -n test checks if a string is not empty.
💻 Command Output
expert
3:00remaining
What is the output of this Bash script with multiple string tests?
Consider this Bash script:
str1=""
str2="data"
if [ -z "$str1" ] && [ "$str2" = "data" ]; then
  echo "Condition met"
else
  echo "Condition not met"
fi
What will it print?
Bash Scripting
str1=""
str2="data"
if [ -z "$str1" ] && [ "$str2" = "data" ]; then
  echo "Condition met"
else
  echo "Condition not met"
fi
ACondition met
BCondition not met
CSyntax error
DNo output
Attempts:
2 left
💡 Hint
Both conditions must be true for the && operator to pass.

Practice

(1/5)
1. Which operator in bash scripting checks if two strings are equal?
easy
A. Using = inside double brackets [[ ]]
B. Using -z to check string length
C. Using != inside double brackets [[ ]]
D. Using -n to check if string is not empty

Solution

  1. Step 1: Understand string equality operator

    In bash, = inside [[ ]] compares if two strings are equal.
  2. Step 2: Differentiate from other operators

    -z checks if string is empty, != checks inequality, -n checks non-empty string.
  3. Final Answer:

    Using = inside double brackets [[ ]] -> Option A
  4. Quick Check:

    String equality = = [OK]
Hint: Use = inside [[ ]] to check if strings are equal [OK]
Common Mistakes:
  • Confusing = with -z or -n
  • Using = outside [[ ]] without quotes
  • Mixing != with = for equality
2. Which of the following is the correct syntax to check if a variable var is not empty in bash?
easy
A. [ -z "$var" ]
B. [ "$var" = "" ]
C. [ -n "$var" ]
D. [ $var != "" ]

Solution

  1. Step 1: Understand -n operator

    -n returns true if the string length is greater than zero (not empty).
  2. Step 2: Check syntax correctness

    Using quotes around $var prevents errors if variable is empty or contains spaces.
  3. Final Answer:

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

    Not empty check = -n [OK]
Hint: Use [ -n "$var" ] to test if var is not empty [OK]
Common Mistakes:
  • Using -z instead of -n for not empty
  • Forgetting quotes around variable
  • Using = "" to check emptiness incorrectly
3. What will be the output of this bash script snippet?
var="hello"
if [[ "$var" != "world" ]]; then
  echo "Not world"
else
  echo "World"
fi
medium
A. Not world
B. World
C. Syntax error
D. No output

Solution

  1. Step 1: Evaluate string comparison

    Variable var is "hello", which is not equal to "world".
  2. Step 2: Determine which branch runs

    The condition [[ "$var" != "world" ]] is true, so it echoes "Not world".
  3. Final Answer:

    Not world -> Option A
  4. Quick Check:

    hello != world -> Not world [OK]
Hint: If strings differ, != condition is true [OK]
Common Mistakes:
  • Confusing = and != operators
  • Missing quotes causing syntax errors
  • Assuming else branch runs when condition is true
4. Identify the error in this bash snippet that checks if input is empty:
if [ -z $input ]; then
  echo "Empty"
else
  echo "Not empty"
fi
medium
A. Using -z instead of -n
B. Missing quotes around $input in test
C. Using single brackets instead of double brackets
D. No error, script is correct

Solution

  1. Step 1: Check variable quoting in test

    Variable $input is not quoted, which can cause errors if empty or contains spaces.
  2. Step 2: Understand correct syntax

    Quotes around $input like "$input" are needed to safely test emptiness.
  3. Final Answer:

    Missing quotes around $input in test -> Option B
  4. Quick Check:

    Always quote variables in tests [OK]
Hint: Always quote variables in [ ] tests to avoid errors [OK]
Common Mistakes:
  • Forgetting quotes around variables
  • Thinking -z and -n are interchangeable
  • Believing single brackets can't test strings
5. You want to write a bash script that prints "Empty string" if variable text is empty, and "Has content" otherwise. Which snippet correctly does this?
hard
A. if [ "$text" != "" ]; then echo "Empty string"; else echo "Has content"; fi
B. if [[ "$text" = "" ]]; then echo "Has content"; else echo "Empty string"; fi
C. if [ -n "$text" ]; then echo "Empty string"; else echo "Has content"; fi
D. if [[ -z "$text" ]]; then echo "Empty string"; else echo "Has content"; fi

Solution

  1. Step 1: Understand -z usage

    -z "$text" returns true if text is empty.
  2. Step 2: Check echo outputs match condition

    If empty, print "Empty string"; else print "Has content". if [[ -z "$text" ]]; then echo "Empty string"; else echo "Has content"; fi matches this logic correctly.
  3. Final Answer:

    if [[ -z "$text" ]]; then echo "Empty string"; else echo "Has content"; fi -> Option D
  4. Quick Check:

    -z empty -> print "Empty string" [OK]
Hint: Use if [[ -z "$var" ]] for empty string check [OK]
Common Mistakes:
  • Swapping outputs for empty and non-empty cases
  • Using = "" without quotes or wrong logic
  • Confusing -n and -z meanings
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard