Bash Script to Compare Two Strings Easily
In Bash, compare two strings using
if [ "$str1" = "$str2" ] to check if they are equal, or if [ "$str1" != "$str2" ] to check if they differ.Examples
Inputstr1='hello', str2='hello'
OutputStrings are equal
Inputstr1='apple', str2='orange'
OutputStrings are not equal
Inputstr1='', str2=''
OutputStrings are equal
How to Think About It
To compare two strings in Bash, you check if they are exactly the same or different using the
= or != operators inside an if statement. This lets the script decide what to do based on whether the strings match.Algorithm
1
Get the first string input2
Get the second string input3
Use an if statement to compare the two strings with the = operator4
If they are equal, print a message saying so5
Otherwise, print a message saying they are not equalCode
bash
#!/bin/bash str1="$1" str2="$2" if [ "$str1" = "$str2" ]; then echo "Strings are equal" else echo "Strings are not equal" fi
Output
Strings are equal
Dry Run
Let's trace comparing 'hello' and 'hello' through the code
1
Assign inputs
str1='hello', str2='hello'
2
Compare strings
Check if 'hello' = 'hello' (true)
3
Print result
Output: Strings are equal
| Step | str1 | str2 | Comparison Result | Output |
|---|---|---|---|---|
| 1 | hello | hello | true | Strings are equal |
Why This Works
Step 1: Use of if statement
The if statement checks a condition and runs code based on whether it is true or false.
Step 2: String comparison operator
The = operator inside [ ] tests if two strings are exactly the same.
Step 3: Output based on comparison
If strings match, the script prints 'Strings are equal'; otherwise, it prints 'Strings are not equal'.
Alternative Approaches
Using double brackets [[ ]]
bash
#!/bin/bash str1="$1" str2="$2" if [[ "$str1" == "$str2" ]]; then echo "Strings are equal" else echo "Strings are not equal" fi
Double brackets [[ ]] allow more flexible string comparison and pattern matching; preferred in modern Bash.
Using test command
bash
#!/bin/bash str1="$1" str2="$2" test "$str1" = "$str2" && echo "Strings are equal" || echo "Strings are not equal"
The test command is a simpler one-liner but less readable for beginners.
Complexity: O(n) time, O(1) space
Time Complexity
Comparing two strings takes time proportional to their length, so it is O(n) where n is the length of the shorter string.
Space Complexity
The comparison uses constant extra space O(1) since it only stores the input strings and comparison result.
Which Approach is Fastest?
All methods use similar string comparison operations; using [[ ]] is preferred for readability and modern syntax.
| Approach | Time | Space | Best For |
|---|---|---|---|
| [ ] with = | O(n) | O(1) | Basic string equality checks |
| [[ ]] with == | O(n) | O(1) | Modern Bash scripts with flexible syntax |
| test command | O(n) | O(1) | Simple one-liners or legacy scripts |
Always quote your variables in comparisons to avoid errors with spaces or empty strings.
Forgetting to quote variables can cause syntax errors or unexpected results when strings contain spaces or are empty.