String comparisons help you check if words or texts are equal, different, empty, or not empty in scripts.
String comparisons (=, !=, -z, -n) in Bash Scripting
if [ "$string1" = "$string2" ]; then # commands fi if [ "$string1" != "$string2" ]; then # commands fi if [ -z "$string" ]; then # commands for empty string fi if [ -n "$string" ]; then # commands for non-empty string fi
Always put strings in double quotes to avoid errors with spaces or empty values.
Use single brackets [ ] with spaces around operators for string tests in bash.
name is exactly "Alice".if [ "$name" = "Alice" ]; then echo "Hello, Alice!" fi
input is not empty.if [ "$input" != "" ]; then echo "You typed something." fi
password has no characters.if [ -z "$password" ]; then echo "Password is empty!" fi
filename has one or more characters.if [ -n "$filename" ]; then echo "Filename is set." fi
This script asks for your name. It checks if you typed nothing, if you typed "Alice", or something else, and prints a message.
#!/bin/bash read -p "Enter your name: " name if [ "$name" = "" ]; then echo "You did not enter a name." elif [ "$name" = "Alice" ]; then echo "Welcome back, Alice!" else echo "Hello, $name!" fi
Use = for equality and != for inequality in string tests.
-z means the string is empty; -n means it is not empty.
Always quote variables to avoid errors when they are empty or contain spaces.
Use = and != to compare if strings are equal or not.
Use -z to check if a string is empty, and -n to check if it is not empty.
Always put quotes around variables in comparisons to keep your script safe.
Practice
Solution
Step 1: Understand string equality operator
In bash,=inside [[ ]] compares if two strings are equal.Step 2: Differentiate from other operators
-zchecks if string is empty,!=checks inequality,-nchecks non-empty string.Final Answer:
Using=inside double brackets [[ ]] -> Option AQuick Check:
String equality = = [OK]
- Confusing = with -z or -n
- Using = outside [[ ]] without quotes
- Mixing != with = for equality
var is not empty in bash?Solution
Step 1: Understand -n operator
-nreturns true if the string length is greater than zero (not empty).Step 2: Check syntax correctness
Using quotes around$varprevents errors if variable is empty or contains spaces.Final Answer:
[ -n "$var" ] -> Option CQuick Check:
Not empty check = -n [OK]
- Using -z instead of -n for not empty
- Forgetting quotes around variable
- Using = "" to check emptiness incorrectly
var="hello" if [[ "$var" != "world" ]]; then echo "Not world" else echo "World" fi
Solution
Step 1: Evaluate string comparison
Variablevaris "hello", which is not equal to "world".Step 2: Determine which branch runs
The condition[[ "$var" != "world" ]]is true, so it echoes "Not world".Final Answer:
Not world -> Option AQuick Check:
hello != world -> Not world [OK]
- Confusing = and != operators
- Missing quotes causing syntax errors
- Assuming else branch runs when condition is true
input is empty:
if [ -z $input ]; then echo "Empty" else echo "Not empty" fi
Solution
Step 1: Check variable quoting in test
Variable$inputis not quoted, which can cause errors if empty or contains spaces.Step 2: Understand correct syntax
Quotes around$inputlike"$input"are needed to safely test emptiness.Final Answer:
Missing quotes around $input in test -> Option BQuick Check:
Always quote variables in tests [OK]
- Forgetting quotes around variables
- Thinking -z and -n are interchangeable
- Believing single brackets can't test strings
text is empty, and "Has content" otherwise. Which snippet correctly does this?Solution
Step 1: Understand -z usage
-z "$text"returns true iftextis empty.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.Final Answer:
if [[ -z "$text" ]]; then echo "Empty string"; else echo "Has content"; fi -> Option DQuick Check:
-z empty -> print "Empty string" [OK]
- Swapping outputs for empty and non-empty cases
- Using = "" without quotes or wrong logic
- Confusing -n and -z meanings
Ready to prove it?
Pass the test and this pattern moves unknown
