×


Compare Numbers in a Bash Script - How to do it ?



Number comparison is considered as an important arithmetic operation that lets you find out the relative magnitude of one number in comparison to another. Bash also allows you to compare numbers very easily.

Here at LinuxAPT, we shall look into how you can use a Bash script for comparing two numbers.


Examples of using Number Comparisons in Bash Scripts

1. Using the Equal and Not Equal Comparison Operators

Here we will use the equal and not equal operators in Bash for comparing two numbers. 

Have a look at the Bash script below: 

#!/bin/bash
var1=22
var2=27
if [ $var1 -eq $var2 ]; then
  echo "var1 and var2 are equal"
fi
if [ $var1 -ne $var2 ]; then
  echo "var1 and var2 are not equal"
fi

Here, we first used the shebang for declaring our document as a Bash script. Then, we defined two variables, "var1" and "var2", and assigned to them to two different integers. Then, we used an "if" statement for checking if these two numbers are equal. If yes, a relevant message is printed on the terminal. After that, we have another "if" statement in which we used the "not equal" operator for checking if these numbers are not equal. If this statement is evaluated to true, a relevant message is printed on the terminal.

Then, after saving the Bash file, run it with the following command:

$ bash compare.sh

After execution, you will see that the second "if" statement turned out to be true since the two specified numbers were not equal


2. Using the Greater Than and Greater Than or Equal To Operators

Here, we will use the greater than and the greater than or equal to operators in Bash for comparing the two numbers.

Have a look at the Bash script below: 

#!/bin/bash
var1=27
var2=27
if [ $var1 -gt $var2 ]; then
  echo "var1 is greater than var2"
fi
if [ $var1 -ge $var2 ]; then
  echo "var1 and var2 are greater that or equal to each other"
fi

Here, we defined the two variables in this script and kept their values equal. After that, we used two "if" conditions. In the first condition, we used the "greater than" operator of Bash while in the second condition, we used the "greater than or equal to" operator of Bash for comparing the two numbers. If the first condition is true, it means that the first number is greater than the second number while if the second condition is true, it means that these two numbers are greater than or equal to each other.

When we ran this Bash script, we figured out that these two numbers are greater than or equal to each other.


3. Using the Less Than and Less Than or Equal To Operators

Here, we want to use the less than and less than or equal to operators in Bash for comparing two numbers.

Have a look at the Bash script below: 

#!/bin/bash
var1=22
var2=27
if [ $var1 -lt $var2 ]; then
  echo "var1 is less than var2"
fi
if [ $var1 -le $var2 ]; then
  echo "var1 and var2 are less that or equal to each other"
fi

Here, we defined the two variables in this Bash script and assigned to them the two different values. Then, we used two "if" conditions. The first one makes use of the "less than" operator while the second one makes use of the "less than or equal to" operator. If the first condition is true, it means that the first variable is less than the second while if the second condition is true, it means that the given variables are less than or equal to each other. 

However, in this case, we have chosen both the numbers in a way that both of these conditions are evaluated to true.


[Need help in fixing Linux System issues ? We can help you.  ]


Conclusion

This article covers the methods of comparing two numbers in Bash. 


Comparison Operators for Numbers:

  • num1 -eq num2: check if 1st  number is equal to 2nd number.
  • num1 -ge num2: checks if 1st  number  is greater than or equal to 2nd number.
  • num1 -gt num2: checks if 1st  number is greater than 2nd number.
  • num1 -le num2: checks if 1st number is less than or equal to 2nd number.
  • num1 -lt num2: checks if 1st  number  is less than 2nd number.
  • num1 -ne num2: checks if 1st  number  is not equal to 2nd number.

More Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on IbmiMedia.com

Also for Tech related tips, Visit forum.outsourcepath.com or General Technical tips on www.outsourcepath.com