Greater Than Numerical Comparison in a Bash Script
There are various ways to compare two numbers in the Bash script, and we will describe them all with some examples:
1. Compare Two Numbers Using > Command
It is a simple command which you can use to find out the greater number in the comparison. For example, you have X=55 and Y=66. You can use the following script to compare X and Y in a condition:
In the given source code, we used the (($X > $Y)) that returns true when the value of X is greater than Y.
The double parentheses are used to create the integer arithmetic operations. It is a built-in feature of the Bash script that returns either 1 for true or zero for false. Now, let’s execute the bash script to get the following result:
Similarly, you can use the greater than or equal comparison using the >= command. When X is greater than Y, it returns true.
This script provides the following result in the terminal:
2. Compare Two Numbers Using -Gt Command
You can use the -gt command in the script to check the greater number in the condition. Here is the Bash script example that you can try:
The -gt command (greater than) checks if one value is greater than the other. Once you run a Bash script, you will get the following result:
In the same way, you can use the –ge (greater than or equal) command to check the greater than or equals to numerical comparison:
You will get the following result by executing the script in the terminal:
Conclusion
This is how you can efficiently perform the greater-than numerical comparison in a Bash script. You can compare variables, strings, and numbers using the script’s > or -gt command. We used the multiple examples to describe the methods to compare two numbers in Bash. Similarly, you can use < or -lt commands to evaluate the less than numerical comparison.