0
0
Bash Scriptingscripting~5 mins

String comparisons (=, !=, -z, -n) in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the '=' operator do in bash string comparisons?
The '=' operator checks if two strings are exactly equal. It returns true if they match.
Click to reveal answer
beginner
How do you check if two strings are NOT equal in bash?
Use the '!=' operator inside a test condition. It returns true if the strings differ.
Click to reveal answer
beginner
What does '-z' check in bash string tests?
The '-z' operator checks if a string is empty (zero length). It returns true if the string has no characters.
Click to reveal answer
beginner
What does '-n' check in bash string tests?
The '-n' operator checks if a string is not empty. It returns true if the string has one or more characters.
Click to reveal answer
beginner
Write a simple bash if statement to check if variable 'name' is empty.
if [ -z "$name" ]; then echo "Name is empty" fi This uses '-z' to test if 'name' has zero length.
Click to reveal answer
Which operator checks if two strings are equal in bash?
A-z
B!=
C=
D-n
What does the '-z' operator test for in bash?
AString is not empty
BString is empty
CStrings are equal
DStrings are not equal
How do you test if two strings are NOT equal in bash?
A!=
B-n
C-z
D=
Which operator returns true if a string is NOT empty?
A-n
B-z
C=
D!=
What will this bash test return? [ -z "hello" ]
ATrue
BError
CDepends on shell
DFalse
Explain how to check if a string variable is empty or not in bash using string comparison operators.
Think about testing zero length strings.
You got /4 concepts.
    Describe the difference between '=' and '!=' operators in bash string comparisons.
    One checks if strings match, the other if they differ.
    You got /4 concepts.