Often in Bash you may want to perform a case-insensitive comparison of strings.
You can use the following syntax to do so:
if [ "${string1,,}" = "${string2,,}" ]
then
echo "Strings are equal"
else
echo "Strings are not equal"
fi
This particular example checks if the strings stored in the variables named string1 and string2 are equal (ignoring case) and returns one of the following strings as a result:
- “Strings are equal”
- “Strings are not equal”
Note that we use the syntax ${string1,,} and ${string2,,} to first convert each string to all lowercase letters.
By doing so, we are able to check if the two strings contain all of the same characters, regardless of case.
The following example shows how to use this syntax in practice.
Example: How to Perform Case-Insensitive Comparison of Strings in Bash
Suppose that we create two variables named string1 and string2 in Bash that contain the following strings:
- Mavericks
- MAVericks
We can use the following syntax to check if the two strings are equal, regardless of case:
string1=Mavericks
string2=MAVericks
if [ "${string1,,}" = "${string2,,}" ]
then
echo "Strings are equal"
else
echo "Strings are not equal"
fi
The following screenshot shows how to use this syntax in practice:

This returns “Strings are equal” since the two strings contain the same letters in the same order, regardless of case.
Suppose instead that we create two variables named string1 and string2 in Bash that contain the following strings:
- Mavericks
- Lakers
We can use the following syntax to check if the two strings are equal, regardless of case:
string1=Mavericks
string2=Lakers
if [ "${string1,,}" = "${string2,,}" ]
then
echo "Strings are equal"
else
echo "Strings are not equal"
fi
The following screenshot shows how to use this syntax in practice:

This returns “Strings are not equal” since the two strings do not contain the same letters in the same order, regardless of case.
Feel free to replace the strings in the variables named string1 and string2 with whatever strings that you would like.
Related Tutorials
The following tutorials explain how to perform other common tasks in Bash:
Bash: How to Compare a String with a Number
Bash: How to Check if String Contains Substring
Bash: How to Split a String and Get Last Element
