0
0
Bash Scriptingscripting~20 mins

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

Choose your learning style9 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.