Bash script: String comparison examples

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 tutorial, we’ll show you how to compare strings in a Bash script 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.

In this tutorial you will learn:

  • How to compare strings in Bash
  • Example if/else Bash scripts that compare strings
An example of comparing two strings in a Bash script on Linux
An example of comparing two strings in a Bash script on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Bash shell (installed by default)
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

Bash script: String comparison examples



Twitter icon Follow LinuxConfig.org on Twitter for the latest tips and tricks about Linux!

  1. In a Bash script, you’d normally be storing one or both of your strings as variables before comparing them. In this example, we are using the = operator and an if statement to determine if the two strings are equal to each other. The if statement will either proceed with its first clause or the else cause, depending on whether or not the strings are equal.
    #!/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

    Here is the result when we execute the script:

    $ ./test.sh
    The two strings are not equal.
    
  2. But = is not the only operator available to us. We could also test to see if two strings are not equal by using the != operator.
    #!/bin/bash
    
    string1="apples"
    string2="oranges"
    
    if [ "$string1" != "$string2" ]; then
    	echo "Strings are different."
    else
    	echo "Strings are not different."
    fi

    Here is the result when we execute the script:

    $ ./test.sh
    Strings are different.
    
  3. Another operator we can use with strings is -z, which allows us to test if the string length is 0.
    #!/bin/bash
    
    string=""
    
    if [[ -z $string ]]; then
    	echo "The string is empty."
    else
    	echo "The string is not empty."
    fi

    Here is the result when we execute the script:

    $ ./test.sh
    The string is empty.
    
  4. The -n operator can also be used to test if the string length is NOT zero.
    #!/bin/bash
    
    string="hello"
    
    if [[ -n $string ]]; then
        echo "The string is not empty."
    else
        echo "The string is empty."
    fi

    Here is the result when we execute the script:

    $ ./test.sh
    The string is not empty.
    
  5. We can also use the less than < and greater than > operators to check if one string has more characters than the other. Here is an example.
    #!/bin/bash
    
    string1="apples"
    string2="oranges"
    
    if [[ "$string1" > "$string2" ]]; then
    	echo "$string1 has more characters than $string2."
    else
    	echo "$string2 has more characters than $string1."
    fi

    Here is the result when we execute the script:



    $ ./test.sh
    oranges has more characters than apples.
    

Closing Thoughts

In this tutorial, we saw how to compare strings in Bash scripting, particularly in the context of 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.



Comments and Discussions
Linux Forum