Skip to content

09. Bash Comparisons

Overview

Bash provides comparison operators that allow you to compare values and determine whether they are equal, greater, smaller, or different.

Comparisons are commonly used with if, elif, and else statements.

Arithmetic Comparisons

Arithmetic comparison operators are used to compare integer values.

Operator Alternative Description
-lt < Less than
-gt > Greater than
-le <= Less than or equal to
-ge >= Greater than or equal to
-eq == Equal to
-ne != Not equal to

Comparing Two Numbers

The following example compares two integers using -eq.

#!/bin/bash

NUM1=2
NUM2=2

if [ $NUM1 -eq $NUM2 ]; then
    echo "Both values are equal"
else
    echo "Values are NOT equal"
fi

Output

$ ./statement.sh
Both values are equal

The -eq operator checks whether NUM1 and NUM2 have the same numerical value.

Comparing Different Numbers

The values can be changed to see how the comparison behaves.

#!/bin/bash

NUM1=2
NUM2=1

if [ $NUM1 -eq $NUM2 ]; then
    echo "Both values are equal"
else
    echo "Values are NOT equal"
fi

Output

$ ./statement.sh
Values are NOT equal

Since 2 is not equal to 1, the condition is false and the else block executes.

Using elif for Multiple Comparisons

Multiple conditions can be checked using elif.

#!/bin/bash

NUM1=2
NUM2=1

if [ $NUM1 -eq $NUM2 ]; then
    echo "Both values are equal"
elif [ $NUM1 -gt $NUM2 ]; then
    echo "NUM1 is greater than NUM2"
else
    echo "NUM2 is greater than NUM1"
fi

Output

$ ./statement.sh
NUM1 is greater than NUM2

The script first checks whether the numbers are equal. If they are not equal, the elif condition checks whether NUM1 is greater than NUM2. If neither condition is true, the else block executes.

String Comparisons

String comparison operators are used to compare text values.

Operator Description
= Strings are equal
!= Strings are not equal
< String is less than another string
> String is greater than another string
-n String is not empty
-z String is empty

Comparing Two Strings

The following example compares two strings using =.

#!/bin/bash

S1="Bash"
S2="Scripting"

if [ $S1 = $S2 ]; then
    echo "Both Strings are equal"
else
    echo "Strings are NOT equal"
fi

Output

$ ./statement.sh
Strings are NOT equal

The strings contain different values, so the condition is false.

Comparing Matching Strings

If both variables contain the same string, the condition becomes true.

#!/bin/bash

S1="Bash"
S2="Bash"

if [ $S1 = $S2 ]; then
    echo "Both Strings are equal"
else
    echo "Strings are NOT equal"
fi

Output

$ ./statement.sh
Both Strings are equal

Since both S1 and S2 contain Bash, the = comparison returns true.

Summary

Bash comparison operators allow scripts to make decisions based on values.

Arithmetic comparisons are used for numbers:

[ $NUM1 -eq $NUM2 ]
[ $NUM1 -gt $NUM2 ]
[ $NUM1 -lt $NUM2 ]

String comparisons are used for text:

[ $S1 = $S2 ]
[ $S1 != $S2 ]

These comparisons are commonly combined with if, elif, and else statements to control the execution flow of Bash scripts.