0
0
Bash-scriptingHow-ToBeginner · 2 min read

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 input
2
Get the second string input
3
Use an if statement to compare the two strings with the = operator
4
If they are equal, print a message saying so
5
Otherwise, print a message saying they are not equal
💻

Code

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

Stepstr1str2Comparison ResultOutput
1hellohellotrueStrings 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.

ApproachTimeSpaceBest For
[ ] with =O(n)O(1)Basic string equality checks
[[ ]] with ==O(n)O(1)Modern Bash scripts with flexible syntax
test commandO(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.