Bash Strings Equal

Bash strings equal means checking whether two string values contain exactly the same characters in the same order. In Bash scripting, you can compare strings with == for equality and != for inequality inside an if statement.

For simple string comparison, use [ "$str1" == "$str2" ] or [[ "$str1" == "$str2" ]]. The double bracket form [[ ... ]] is usually safer in Bash scripts because it handles many quoting and pattern-matching cases more predictably.

Bash string comparison operators for equal and not equal checks

Operator or testMeaning in Bash string comparisonExample
==Returns true when two strings are equal.[[ "$a" == "$b" ]]
=Also works for string equality. It is common with single brackets.[ "$a" = "$b" ]
!=Returns true when two strings are not equal.[[ "$a" != "$b" ]]
-zReturns true when a string is empty.[[ -z "$a" ]]
-nReturns true when a string is not empty.[[ -n "$a" ]]

To check if two strings are equal in bash scripting, use bash if statement and double equal to ==  operator.

To check if two strings are not equal in bash scripting, use bash if statement and not equal to !=  operator.

Bash if statement syntax to compare two strings

The basic Bash syntax for checking whether two strings are equal is shown below.

</>
Copy
if [[ "$string1" == "$string2" ]]; then
    # commands when both strings are equal
else
    # commands when both strings are not equal
fi

The syntax for checking whether two strings are not equal is similar, but it uses !=.

</>
Copy
if [[ "$string1" != "$string2" ]]; then
    # commands when strings are different
else
    # commands when strings are equal
fi

In Bash, quote string variables while comparing them. Quoting prevents errors when a variable is empty or contains spaces.

Example 1 – Strings Equal Scenario

In this example program, we will check if two strings are equal using IF statement and Equal-to operator.

Bash Script File

</>
Copy
#!/bin/bash

str1="Learn Bash"
str2="Learn Bash"

if [ "$str1" == "$str2" ]; then
	echo "Both Strings are Equal."
else
	echo "Both Strings are not Equal."
fi

Output

~/workspace/bash$ ./bash-strings-equal-example 
Both Strings are Equal.

The strings stored in str1 and str2 are identical, so the condition evaluates to true and the first echo command runs.

Example 2 – Strings Not Equal Scenario

In this example program, we will check if two strings are not equal using IF statement and Not-Equal-to operator.

Bash Script File

#!/bin/bash

str1="Learn Bash"
str2="Learn Bash with tutorialkart"

if [ "$str1" != "$str2" ]; then
	echo "Both Strings are not Equal."
else
	echo "Both Strings are Equal."
fi

Output

~/workspace/bash$ ./bash-strings-equal-example-1 
Both Strings are not Equal.

Here, str1 and str2 contain different text. Since the != condition is true, Bash prints that both strings are not equal.

Bash compare strings with double brackets [[ ]]

In modern Bash scripts, [[ ... ]] is commonly used for string tests. It is a Bash keyword, not an external command, and it avoids several common parsing issues that can occur with the older single bracket form.

</>
Copy
#!/bin/bash

expected="admin"
actual="admin"

if [[ "$actual" == "$expected" ]]; then
    echo "User role matches."
else
    echo "User role does not match."
fi
User role matches.

Use [[ ... ]] when your script is intended to run with Bash. If the script must be portable to a plain POSIX shell, use [ ... ] with = for equality.

Bash compare string variable with a fixed text value

Another common requirement is to compare a variable with a literal string. The literal value can be written directly in the condition.

</>
Copy
#!/bin/bash

environment="production"

if [[ "$environment" == "production" ]]; then
    echo "Production settings will be used."
else
    echo "Non-production settings will be used."
fi
Production settings will be used.

This form is useful for checking command options, environment names, file labels, status values, and other text fields inside Bash scripts.

Bash string equality is case-sensitive

Bash string comparison is case-sensitive by default. The strings Linux and linux are not equal because the uppercase L and lowercase l are different characters.

</>
Copy
#!/bin/bash

str1="Linux"
str2="linux"

if [[ "$str1" == "$str2" ]]; then
    echo "Strings are equal."
else
    echo "Strings are different."
fi
Strings are different.

For a case-insensitive comparison in Bash 4 and later, convert both values to the same case before comparing them.

</>
Copy
#!/bin/bash

str1="Linux"
str2="linux"

if [[ "${str1,,}" == "${str2,,}" ]]; then
    echo "Strings are equal after converting to lowercase."
else
    echo "Strings are different."
fi
Strings are equal after converting to lowercase.

Bash check whether a string is empty before comparing

When values come from user input, command output, or environment variables, check whether a string is empty before using it in later logic. Bash provides -z for empty strings and -n for non-empty strings.

</>
Copy
#!/bin/bash

username=""

if [[ -z "$username" ]]; then
    echo "Username is empty."
else
    echo "Username is $username"
fi
Username is empty.

Use -n when you want the opposite check.

</>
Copy
if [[ -n "$username" ]]; then
    echo "Username is not empty."
fi

Bash pattern matching while comparing strings with ==

Inside [[ ... ]], the right side of == can work as a pattern when it is not quoted. This is useful when you want to check whether a string starts with, ends with, or contains a certain pattern.

</>
Copy
#!/bin/bash

filename="report-2026.csv"

if [[ "$filename" == *.csv ]]; then
    echo "CSV file detected."
else
    echo "This is not a CSV file."
fi
CSV file detected.

If you need an exact string comparison, quote both values. If you need pattern matching, leave the pattern unquoted intentionally.

Bash string comparison mistakes to avoid

  • Do not use numeric operators for strings: use == or != for strings, not -eq or -ne.
  • Quote variables in single bracket tests: use [ "$a" = "$b" ] to avoid errors when a variable is empty or contains spaces.
  • Keep spaces around brackets: write [ "$a" = "$b" ], not ["$a"="$b"].
  • Understand pattern matching in [[ ... ]]: an unquoted right-hand side can act as a pattern.
  • Use the correct shell: scripts that use [[ ... ]] should run with Bash, so start them with #!/bin/bash.

Bash strings equal FAQs

How do I check if two strings are equal in Bash?

Use [[ "$str1" == "$str2" ]] inside an if statement. If both strings contain the same characters in the same order, the condition is true.

Should I use = or == for Bash string equality?

Both can work in Bash. The == operator is commonly used with [[ ... ]], while = is widely used with the portable single bracket form [ ... ].

How do I check if two Bash strings are not equal?

Use the != operator. For example, [[ "$str1" != "$str2" ]] returns true when the two strings are different.

Is Bash string comparison case-sensitive?

Yes. Bash string comparison is case-sensitive by default. For example, Admin and admin are different strings unless you convert both values to the same case before comparing.

Why does my Bash string comparison fail when the variable is empty?

With single brackets, unquoted empty variables can break the test expression. Quote variables, such as [ "$value" = "yes" ], or use the Bash double bracket form [[ "$value" == "yes" ]].

Bash strings equal QA checklist

  • Confirm that string comparisons use ==, =, or !=, not numeric operators such as -eq.
  • Check that variable references are quoted in examples where spaces or empty values are possible.
  • Verify that examples using [[ ... ]] are clearly described as Bash-specific.
  • Include both equal and not-equal scenarios so readers can adapt the condition in either direction.
  • Test examples with matching strings, different strings, empty strings, and different letter cases.

Summary: Bash string equality with == and !=

In this Bash TutorialBash Strings Equal, we have learnt to check if two strings are equal or not with the help of double equal to and not equal to operators.

Use [[ "$str1" == "$str2" ]] when you want to check whether two Bash strings are equal, and use [[ "$str1" != "$str2" ]] when you want to check whether they are different. For robust scripts, quote variables, use -z or -n for empty string checks, and remember that Bash string comparison is case-sensitive by default.