Beginners Guide To BASH - Comparing Things

01
of 08

Beginners Guide To BASH - Comparing Things

BASH Tutorial - Comparing Strings
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 week's 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 I have 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 output the string "the names match" and if they don't output 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" ]

02
of 08

Beginners Guide To BASH - Comparing Strings

BASH Tutorial - 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 I compared "$name1" \< "$name2".

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

For example

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

03
of 08

Beginners Guide To BASH - Comparing Strings

BASH Tutorial - Comparing Strings
BASH Tutorial - Comparing Strings.

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

if [ -n $name2 ]

In the script above I have tested whether $name2 has been given a value and if it doesn't the message "There is no bob, there was never a bob appears".

04
of 08

Beginners Guide To BASH - Comparing Strings

BASH Tutorial - Comparing Strings
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.

05
of 08

Beginners Guide To BASH - Comparing Numbers

BASH Tutorial - Comparing Numbers
BASH Tutorial - Comparing Numbers.

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

The above 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.

I prefer however to use the following operator to compare two numbers:

If [ $a -eq $b ]

06
of 08

Beginners Guide To BASH - Comparing Numbers

BASH Tutorial - Comparing Numbers
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 ]

07
of 08

Beginners Guide To BASH - Comparing Numbers

BASH Tutorial - Comparing Numbers
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 ]

08
of 08

Beginners Guide To BASH - Comparison Operators - Summary

If you have missed the first three parts of this guide you can find them by clicking on the following links:

In the next part of the guide I will be covering arithmetic.