0
0
Bash Scriptingscripting~10 mins

String comparisons (=, !=, -z, -n) in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - String comparisons (=, !=, -z, -n)
Start
Get two strings
Check if strings are equal (=)
YesPrint 'Equal'
No
Check if strings are not equal (!=)
YesPrint 'Not Equal'
No
Check if string is empty (-z)
YesPrint 'Empty String'
No
Check if string is not empty (-n)
YesPrint 'Not Empty'
No
End
The flow checks string equality, inequality, emptiness, and non-emptiness step-by-step, printing results accordingly.
Execution Sample
Bash Scripting
str1="hello"
str2="world"
if [ "$str1" = "$str2" ]; then
  echo "Equal"
elif [ "$str1" != "$str2" ]; then
  echo "Not Equal"
fi
This script compares two strings and prints if they are equal or not.
Execution Table
StepCondition CheckedEvaluationBranch TakenOutput
1[ "$str1" = "$str2" ]"hello" = "world" → FalseNo
2[ "$str1" != "$str2" ]"hello" != "world" → TrueYesNot Equal
3End of conditions---
💡 Condition for inequality is true, so 'Not Equal' is printed and script ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
str1"hello""hello""hello""hello"
str2"world""world""world""world"
Key Moments - 3 Insights
Why does the script print 'Not Equal' even though we only checked equality first?
Because the first condition [ "$str1" = "$str2" ] is false (see step 1 in execution_table), the script moves to the elif which checks inequality and finds it true (step 2), so it prints 'Not Equal'.
What does the -z test do and when would it be true?
The -z test checks if a string is empty. It returns true if the string length is zero. For example, if str1="", then [ -z "$str1" ] is true.
Why do we put variables in double quotes inside [ ]?
Double quotes prevent errors if the variable is empty or contains spaces. Without quotes, the test might fail or behave unexpectedly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of the first condition check at step 1?
AFalse
BTrue
CError
DNot evaluated
💡 Hint
Check the 'Evaluation' column in row 1 of execution_table.
At which step does the script print 'Not Equal'?
AStep 1
BStep 3
CStep 2
DNever
💡 Hint
Look at the 'Output' column in execution_table rows.
If str1 was empty (""), which test would return true?
A[ "$str1" = "$str2" ]
B[ -z "$str1" ]
C[ "$str1" != "$str2" ]
D[ -n "$str1" ]
💡 Hint
Refer to key_moments explanation about -z test.
Concept Snapshot
String comparisons in bash:
- Use [ "$a" = "$b" ] for equality
- Use [ "$a" != "$b" ] for inequality
- Use [ -z "$a" ] to check if string is empty
- Use [ -n "$a" ] to check if string is not empty
Always quote variables to avoid errors.
Full Transcript
This visual execution trace shows how bash scripts compare strings using =, !=, -z, and -n. The script starts by assigning two strings, then checks if they are equal. If not equal, it checks inequality and prints 'Not Equal'. The -z test checks if a string is empty, and -n checks if it is not empty. Variables are quoted to prevent errors. The execution table details each step's condition, evaluation, branch, and output. Key moments clarify common confusions like why the script prints 'Not Equal' and the purpose of quoting variables. The quiz tests understanding of condition results and string emptiness checks.