Compare string in BASH

The need to compare strings in a Bash script is relatively common and can be used to check for certain conditions before proceeding on to the next part of a script. A string can be any sequence of characters. To test if two strings are the same, both strings must contain the exact same characters and in the same order. It could be a word or a whole sentence. For example, string one is equal to string one but is not equal to string two. Get the idea?

In this guide, we’ll show you how to compare strings in the Bash shell on a Linux system. We’ll show this in the context of a simple if/else Bash script so you can see how testing for this condition would work when developing scripts, but we’ll also show how this same comparison can be done in the command line terminal.

In this tutorial you will learn:

  • How to compare strings in Bash
  • Example if/else Bash scripts that compare strings

Comparing strings in Bash

Comparing strings in Bash

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Compare if two strings are equal

You can open a terminal on your system and use some of these examples to get a feel for how Bash operators work when it comes to comparing strings.

You can use the following syntax to compare two strings.

$ [ "apples" = "apples" ]
$ echo $?
0

The returned value of 0 means true. In other words, the strings match (as we can clearly see for ourselves).



Let’s try another.

$ [ "apples" = "oranges" ]
$ echo $?
1

In this example, apples does not equal oranges, so a value of 1 (false) is returned. More complex examples are best shown in the context of Bash scripts, which we cover in the next section.

Example Bash scripts to compare strings

In a Bash script, you’d normally be storing one or both of your strings as variables before comparing them. Here’s a simple example.

#!/bin/bash

string1="apples"
string2="oranges"

if [ "$string1" = "$string2" ]; then
	echo "The two strings are equal."
else
	echo "The two strings are not equal."
fi

Executing this script will produce the following output.

The two strings are not equal.

But = isn’t the only operator available to us. We could also test to see if two strings are not equal with the != operator.

#!/bin/bash

string1="apples"
string2="oranges"

if [ "$string1" != "$string2" ]; then
	echo "Strings are different."
else
	echo "Strings are not different."
fi

Executing this script will produce the following output.

Strings are different.

There’s also -z to test if the string length is 0, and -n to test if the string length is non-zero.

#!/bin/bash

string=""

if [[ -z $string ]]; then
	echo "The string is empty."
else
	echo "The string is not empty."
fi

Executing this script will produce the following output.

The string is empty.

And the same script with -n and a non-zero string instead:



#!/bin/bash

string="hello"

if [[ -n $string ]]; then
    echo "The string is not empty."
else
    echo "The string is empty."
fi

Executing this script will produce the following output.

The string is not empty.

Conclusion

In this guide, we saw how to compare strings in Bash, both from command line and in if/else Bash scripts. This functionality can, of course, be extended to more robust scripts that read input from users or use the case operator, etc.

These are all the comparison methods you should need for comparing strings in Bash. Even more exist when comparing numbers, such as the -lt (less than) and -gt (greater than) operators. But we cover that in greater detail in our Bash scripting guide.



Comments and Discussions
Linux Forum