How to Compare Numbers or Integers in Bash


Written By - admin
Advertisement

In my earlier article I gave you an overview and difference between strings and integers in bash. Now there are different comparison operators which can be used to compare variables containing numbers, which we will learn in this tutorial guide. Being a system administrator you may come across many such requirement where you will need to write a script to perform certain task and iterate the script based on certain numerical values which is where we can use use these operators to compare numbers in bash.

 

There are two different sets of Integer comparison operator which can be used depending upon your requirement.

  • Within square braces
  • Within double parenthesis

 

Comparison Operators for Integers or Numbers

Operator Name Syntax with Square Brackets Syntax with double parenthesis Comment
is equal to [ $INT1 -eq $INT2 ]
OR
[[ $INT1 -eq $INT2 ]]
== Returns TRUE if both the integers in comparison are equal
is not equal to [ $INT1 -ne $INT2 ]
OR
[[ $INT1 -ne $INT2 ]]
!= Returns TRUE if both integers in comparison are not equal
is greater than [ $INT1 -gt $INT2 ]
OR
[[ $INT1 -gt $INT2 ]]
(( $INT1 > $INT2 )) Returns TRUE if left integer is greater than right integer
is less than [ $INT1 -lt $INT2 ]
OR
[[ $INT1 -lt $INT2 ]]
(( $INT1 < $INT2 )) Returns TRUE if left integer is smaller in value than the right integer
is greater than or equal to [ $INT1 -ge $INT2 ]
OR
[[ $INT1 -ge $INT2 ]]
(( $INT1 >= $INT2 )) Returns TRUE is the left operand if either greater than on equal to the right operand value
is less than or equal to [ $INT1 -le $INT2 ]
OR
[[ $INT1 -le $INT2 ]]
(( $INT1 <= $INT2 )) Returns TRUE if left operand if lesser or equal in value to the right operand

 

1. Integer comparison operators within Square Braces

These comparison operators must be used within single or double square braces [ ] or [[ ]]

1.1 Check if integers are equal (-eq)

I will write a basic script to compare the numbers from two different variables. Here both my integer variables have same number, but let's verify this using comparison operator:

INT1=100
INT2=100
if [ $INT1 -eq $INT2 ]; then
  echo "exit status: $?"
  echo "Both integers are equal"
else
  echo "exit status: $?"
  echo "Both integers are not equal"
fi

The output from this script returns zero exit status as both the variables have same number.

exit status: 0
Both integers are equal

 

1.2 Compare variables with different numbers using (-ne)

In this sample script we will use -ne operator to check and compare variables. Here both my variables have different numbers assigned

INT1=100
INT2=101
if [ $INT1 -ne $INT2 ]; then
  echo "exit status: $?"
  echo "Both integers are not equal"
else
  echo "exit status: $?"
  echo "Both integers are equal"
fi

The output from this script returns zero exit status for the first if condition.

exit status: 0
Both integers are not equal

 

1.3 Compare integer values using (-gt) and (-lt)

To check if the numbers in an variable are greater than or less than each other we use -gt or -lt operator. In this example we know that INT1 is greater than INT2 but let us verify this using comparison operators

Advertisement
INT1=101
INT2=100
if [ $INT1 -gt $INT2 ]; then
  echo "exit status: $?"
  echo "\$INT1 is greater than \$INT2"
else
  echo "exit status: $?"
  echo "\$INT2 is greater than \$INT1"
fi

The output from this script. The first condition returns TRUE with zero exit status.

exit status: 0
$INT1 is greater than $INT2

If I modify my variable values, now INT1 is lesser than INT2 but let's verify this using our comparison operator:

INT1=99
INT2=100
if [ $INT1 -lt $INT2 ]; then
  echo "exit status: $?"
  echo "\$INT1 is lesser than \$INT2"
else
  echo "exit status: $?"
  echo "\$INT2 is lesser than \$INT1"
fi

The output from this script now returns zero status for first condition where -lt is TRUE

exit status: 0
$INT1 is lesser than $INT2

 

1.4 Compare integer values using (-ge) and (-le)

We normally use -ge and -le in loop conditions wherein the script will perform a certain task until the loop completes.
I will take a basic if and else condition script to verify return status of both these comparison operator

INT1=101
INT2=100
if [ $INT1 -ge $INT2 ]; then
  echo "exit status: $?"
  echo "\$INT1 is greater than or equal to \$INT2"
else
  echo "exit status: $?"
  echo "\$INT2 is greater than or equal to \$INT1"
fi

The output from this script returns TRUE for first condition

exit status: 0
$INT1 is greater than or equal to $INT2

In the same script I will modify the values of both the variables to verify less than equal to operator

INT1=99
INT2=100
if [ $INT1 -le $INT2 ]; then
  echo "exit status: $?"
  echo "\$INT1 is lesser than or equal to \$INT2"
else
  echo "exit status: $?"
  echo "\$INT2 is lesser than or equal to \$INT1"
fi

The output from this script

exit status: 0
$INT1 is lesser than or equal to $INT2

 

2. Integer comparison operators within Double Parenthesis

These comparison operator must be used double parenthesis (( ))

2.1 Compare integers using (<) or (>)

in this sample script we compare our variables using comparison operator under double parenthesis

INT1=99
INT2=100
if (( $INT1 < $INT2 )); then
  echo "exit status: $?"
  echo "\$INT1 is lesser than \$INT2"
else
  echo "exit status: $?"
  echo "\$INT2 is lesser than \$INT1"
fi

The output from this script

exit status: 0
$INT1 is lesser than $INT2

Similarly to verify the greater than (>) operator:

INT1=99
INT2=100
if (( $INT1 > $INT2 )); then
  echo "exit status: $?"
  echo "\$INT1 is greater than \$INT2"
else
  echo "exit status: $?"
  echo "\$INT2 is greater than \$INT1"
fi

The output from this script

Advertisement
exit status: 1
$INT2 is greater than to $INT1

 

2.2 Compare integers using (<=) or (>=)

These comparison operators are mostly used in loop conditions in real time environments.

INT1=99
INT2=100
if (( $INT1 <= $INT2 )); then
  echo "exit status: $?"
  echo "\$INT1 is lesser than or equal to \$INT2"
else
  echo "exit status: $?"
  echo "\$INT2 is lesser than or equal to \$INT1"
fi

The output from this script, since INT1 is lesser than INT2 in this example the first condition has returned TRUE

exit status: 0
$INT1 is lesser than or equal to $INT2

In the same script I will modify the integer values:

INT1=99
INT2=100
if (( $INT1 >= $INT2 )); then
  echo "exit status: $?"
  echo "\$INT1 is greater than or equal to \$INT2"
else
  echo "exit status: $?"
  echo "\$INT2 is greater than or equal to \$INT1"
fi

The output from this script changes accordingly and now INT2 is reported greater than INT1

exit status: 1
$INT2 is greater than or equal to $INT1

 

Conclusion

Bash is still one of the most used programming language, although we don't prefer bash for writing complicated scripts but for normal scripts such as taking backup for automating certain day to day tasks, bash is still preferred across Linux users.
Comparing integer values in variables is something which is required to be performed in most of the shell scripts.

Lastly I hope the steps from the tutorial guide to understand different comparison operators for integers on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

 

References

I have used below external references for this tutorial guide
Comparison Operators
Arithmetic Expression

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment