Beginner's Guide To BASH & Comparing Things

Compare text and values in your BASH scripts

Comparing Things

BASH Tutorial - Comparing Strings

In the previous part of the BASH tutorial we looked at conditional statements.

That guide was very long but really only showed how to control the flow of logic. This guide shows the different ways in which you can compare variables.

The above image shows the first example in this guide:

#!/bin/bash

name1="gary"
name2="bob"

if [ "$name1" = "$name2" ]; then
echo "the names match"
else
echo "the names do not match"
fi


In the above script has defined two variables called name1 and name2 and assigned them the values "gary" and "bob." As the variables are contained between quotation marks they are called string variables which becomes more relevant as the tutorial goes on.

All the script does is compare the value of $name1 and $name2, and if they match outputs the string "the names match." If they don't it outputs the string "the names do not match."

The quotation marks around the $name1 and $name2 variables are important because if the value of either of them has not been set then the script will still work.

For instance if $name1 was never set then you would be comparing "" with "bob". Without the quotation marks you would be left with = "bob" which clearly fails.

You can also use the != notation to define not equal to as follows:

if [ "$name1" != "$name2" ]

Comparing Strings

BASH Tutorial - Comparing Strings

In the above example, the test compares the same two strings and asks the question does Gary come before Bob in the alphabet?

Clearly, the answer is no.

The script introduces the less than operator (<). As the less than operator is also used for redirection you have to escape it with a slash (\) for it to mean less than, which is why in the script above uses "$name1" \< "$name2".

The opposite of less than is obviously greater than. Instead of using \< use \>.

For example

if [ "$name1" \> "$name2" ]

Comparing Strings (Part 2)

BASH Tutorial - Comparing Strings
BASH Tutorial - Comparing Strings.   Gary Newell, Lifewire, 2018

If you want to test whether a variable has a value you can use the following test:

if [ -n $name2 ]

The script above testes whether $name2 has been given a value and if it doesn't the message "There is no Bob, there was never a Bob appears."

Comparing Strings (Part 3)

BASH Tutorial - Comparing Strings

On the past slide, we covered whether a variable has been set or not. Sometimes, though, a variable may have been set but it might not actually have a value.

For instance:

name1 = ""

To test whether a variable has a value or not (i.e. has a length of zero) use -z as follows:

if [ -z $name1 ] 

In the above script I have set $name1 to a zero length string and then compared it using -z. If $name1 is zero in length the message "gary has gone out for the evening" will be displayed.

Comparing Numbers

BASH Tutorial - Comparing Numbers

Thus far all the comparisons have been for strings. What about comparing numbers?

This script shows an example of comparing two numbers:

#!/bin/bash

a=4
b=5

if [ $a = $b ]; then
echo "4 = 5"
else
echo "4 does not equal 5"
fi


To set a variable to be a number simply set it without quotation marks. You can then compare the numbers with an equals sign.

The following operator tends to be more accurate and clearer to read when comparing two numbers:

if [ $a -eq $b ]

Comparing Numbers (Part 2)

BASH Tutorial - Comparing Numbers

If you want to compare whether a number is less than another number you can use the less than operator (<). As with strings you have to escape the less than operator with a slash. (\<).

A better way of comparing numbers is to use the following notation instead:

  • Less than -lt
  • Less than or equal to -le
  • Greater than -gt
  • Greater than or equal to -ge

For example:

if [ $a -lt $b ]

if [ $a -le $b ]

if [ $a -ge $b ]

if [ $a -gt $b ]

Comparing Numbers (Part 3)

BASH Tutorial - Comparing Numbers

Finally for this guide, if you want to test whether two numbers are different you can use either the less than and greater than operators together (<>) or -ne as follows:

if [ $a <> $b ]

if [ $a -ne $b ]

Comparison Operators Summary

If you have missed the first three parts of this guide you can begin with the BASH introduction, then learn about input parameters, and finally learn about conditions and variables.

In the next part of the guide, we will be covering arithmetic.