Number Comparison Operators in Bash

This article will introduce the Number comparison operators in Bash.

You can use the following comparison operators to compare Integers in a conditional statement.

Integer comparison Description Example
-eq is equal to if [ "$a" -eq "$b" ]
-ne is not equal to if [ "$a" -ne "$b" ]
-gt is greater than if [ "$a" -gt "$b" ]
-ge is greater than or equal to if [ "$a" -ge "$b" ]
-lt is less than if [ "$a" -lt "$b" ]
-le is less than or equal to if [ "$a" -le "$b" ]
< is less than (("$a" < "$b"))
<= is less than or equal to (("$a" <= "$b"))
> is greater than (("$a" > "$b"))
>= is greater than or equal to (("$a" >= "$b"))

1. Example

In this example, It compares Integers using -eq.

#!/bin/bash

a=10
b=10

if [ "$a" -eq "$b" ]
then
    echo "a is equal to b"
fi

Output:

$ bash example.sh
a is equal to b

2. Example

In this example, It compares Integers using <.

#!/bin/bash

c=10
d=20

if (( "$c" < "$d" ))
then
    echo "c is less than d"
fi

Output:

$ bash example.sh
c is less than d
codechachaCopyright ©2019 codechacha