How to Compare Numbers in Bash With If Statement [2 Methods]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

Compare Numbers in Bash scripting is essential for a wide range of tasks, including arithmetic calculations and logical decisions. Therefore, it is fundamental for any developer to understand how the bash can compare numbers in order to facilitate these operations.

In this article, I will provide you with 2 reliable methods to compare numbers in Bash, ensuring seamless scripting for your specific needs. Explore now for expert insights.

Basics of Number Comparison

A number comparison is a way of comparing numbers to figure out the relationship between them. For example, you can compare numbers to find out if they are equal, not equal, greater than, or less than. In order to achieve these objectives, bash provides two types of brackets: square brackets and double parenthesis.

In addition, to compare numbers in bash, there are numerous comparison operators like greater than or equal (>=), less than (<), greater than (-gt), not equal to (!=), equal to (-eq), and more that should be used inside the square brackets or parenthesis of the if statement. The if statement will execute a set of statements if the condition is true or false.

1. Compare Numbers in Bash Using Square Brackets

In Bash scripting, square brackets are used in conjunction with the if-else statement to evaluate various conditions. You can use both single “[“ and double square brackets “[[“, but if you want to be more advanced, double square brackets “[[“ are the way to go.

To compare numbers in bash, there are 6 different comparison operators: -gt, -lt, -eq, -ne, -ge, and -le.

Using “-gt” Option

The -gt option tests if a number is greater than another. The expression if [[ $number1 -gt $number2 ]] checks if number1 is greater than number2 and evaluates to either true or false depending on the input.

So, to compare numbers in Bash, you can use the -gt option inside the double square brackets. Here’s how it works:

#!/bin/bash
echo "Enter an input number1: "
read number1
echo "Enter an input number2: "
read number2
if [[ $number1 -gt $number2 ]];
 then
    echo "Number1 is greater than number2"
 else
    echo "Number1 is not greater than number2"
fi

If the number1 is greater than the number2, it will execute the if block code “Number1 is greater than number2” otherwise it will execute the else block code “Number1 is not greater than number2”.

using -gt option to compare numbers in bash

In this image, you can see that while giving input number1=10 and input number2=8, it shows that “Number1 is greater than number2” as it’s true. When you give number1=2 and input number2=5, it shows that “Number1 is not greater than number2” as 2 is less than 5.

Using “-lt” Option

-lt is a less-than operator that compares numbers in bash by checking if the number1 is less than the number2.

To compare numbers with the -lt option, use this syntax if [ $number1 -lt $number2 ]. See the script below:

#!/bin/bash
echo "Enter an input number1: "
read number1
echo "Enter an input number2: "
read number2
if [ $number1 -lt $number2 ];
 then
    echo "Number1 is less than number2"
 else
    echo "Number1 is not less than number2"
fi

If the input number1 is less than the input number2. It returns true and prints “Number1 is less than number2”. On the other hand, it prints “Number1 is not less than number2”.

using -lt option to compare numbers in bash

The output says that if you take the number1= 5 and number2= 3, it’ll say that number1 is not less than number2 because 5 is greater than 2. But if you take number1= 3 and number2= 6, it just says that number1 is less than number2 since it’s correct.

Using“-eq” Option

-eq option checks if two numbers are equal in their values. The script shows the use of it:

#!/bin/bash
echo "Enter an input number1: "
read number1
echo "Enter an input number2: "
read number2
if [[ $number1 -eq $number2 ]];
 then
    echo "Number1 is equal to number2"
 else
    echo "Number1 is not equal to number2"
fi

If both input numbers are equal, it returns a true expression block “Number1 is equal to number2” otherwise, it returns a false expression block.

using -eq option to compare numbers in bash

You can observe that if both numbers are equal to 5, then executes Number1 is equal to Number2. Conversely, if different numbers such as 4 and 5 are inputted, it indicates that Number1 is different from Number2.

Using “-ne” Option

Unlike -eq, the -ne checks whether the number1 is not equal to the number2. The below script shows how to use -ne within if statement to compare the inequality of two numbers:

#!/bin/bash
echo "Enter an input number1: "
read number1
echo "Enter an input number2: "
read number2
if [ $number1 -ne $number2 ]; then
    echo "Number1 is not equal to number2"
 else
    echo "Number1 is equal to number2"
fi

If the input numbers are not equal, it will execute the if code block “Number1 is not equal to number2”. Contrarily, it will execute the else code block.

using -ne option to compare numbers in bash

When you take both of the inputs as 4, you can see that Number1 is the same as Number2. But if you take 3 and 4, it shows that Number1 is not equal to Number2.

Utilizing “-ge” Option

The expression -ge prints the true output if a number is equal to or greater than another number. This is a bash script that uses the [[ $number1 -ge $number2 ]]  condition to compare numbers with the -ge option:

#!/bin/bash
echo "Enter an input number1: "
read number1
echo "Enter an input number2: "
read number2
if [[ $number1 -ge $number2 ]];
 then
    echo "Number1 is greater than and equal to number2"
 else
    echo "Number1 is not greater than and equal to number2"
fi

The if condition evaluates to true if number1 is greater than or equal to number2. If it returns false, it executes “Number1 is not greater than and equal to number2”.

using -ge option to compare numbers in bash

It displays “Number1 is greater than and equal to number2” after giving both input numbers as 10. It shows different outputs when you give different input numbers.

Using “-le” Option

The -le option works in the same way as -ge, except that it checks whether the number is less than or equal to another number. Here’s how to use it in a script:

#!/bin/bash
echo "Enter an input number1: "
read number1
echo "Enter an input number2: "
read number2
if [[ $number1 -le $number2 ]];
 then
    echo "Number1 is less than and equal to number2"
 else
    echo "Number1 is not less than and equal to number2"
fi

Here when number1 is less than or equal to number2, it will execute the if block expression. On the other hand, it will execute the else block expression.

using -le option to compare numbers in bash

When input number1 and number2 are 20, it displays “Number1 is less than and equal to number2”. If the number1 is greater than or not equal to number2, it shows the “Number1 is not less than and equal to number2”.

Summary of Operators to Compare Numbers in Bash

The following comparison operators are used within square brackets to compare integer numbers in Bash.

Comparison Operator Syntax Description
Greater than (-gt) [[ $number1 -gt $number2 ]] or
[ $number1 -gt $number2 ]
Returns true if the number1 is greater than number2.
Less than (-lt) [[ $number1 -lt $number2 ]] or
[ $number1 -lt $number2 ]
Returns true if the number1 is less than number2.
Equal to (-eq) [[ $number1 -eq $number2 ]] or
[ $number1 -eq $number2 ]
Returns true if the number1 is equal to number2.
Not Equal to (ne) [[ $number1 -ne $number2 ]] or
[ $number1 -ne $number2 ]
Returns true if the number1 is not equal to number2.
Greater than or Equal to (ge) [[ $number1 -ge $number2 ]] or
[ $number1 -ge $number2 ]
Returns true if the number1 is greater than or equal to number2.
Less than or Equal to (le) [[ $number1 -le $number2 ]] or
[ $number1 -le $number2 ]
Returns true if the number1 is less than and equal to number2.
Note: Make sure to double-check the syntax to prevent the bash script from making syntax errors.

2. Compare Numbers in Bash Using Double Parenthesis

Double parenthesis is a built-in arithmetic feature that offers a more flexible syntax to compare numbers in Bash compared to square braces. Moreover, it does not show any errors, even when the space within the parenthesis is removed. In this section, I will be discussing 6 comparison operators. Let’s review them.

Using Greater Than Sign “>”

To compare numbers in bash,  you can apply greater than sign > inside the double parenthesis of the if condition. The condition will come up with a true result if the 1st input number is greater than the 2nd input number. Now. let’s see the bash script:

#!/bin/bash
echo "Enter an input number1: "
read number1
echo "Enter an input number2: "
read number2
if (( $number1 > $number2 ));
 then
    echo "Number1 is greater than number2"
 else
    echo "Number1 is not greater than number2"
fi

If the number1 is greater than number2, it will return true expression “Number1 is greater than number2”. On the other hand, it will return the false code block.

using greater than sign option to compare numbers in bash

This image demonstrates that “Number1 is greater than number2” after writing number1=5 and number2=3. Conversely, number1=4 and number2=8, indicate that “Number1 is not greater than number2” as 4 is less than 8.

Using the Less Than Sign “<”

The less-than sign < compares if a number is less than the other. Like -le, it displays if block code when the 1st operand is less than the 2nd one.

To compare numbers with less than sign, use this syntax (( $number1 < $number2 )) within the if condition. Here’s how:

#!/bin/bash
echo "Enter an input number1: "
read number1
echo "Enter an input number2: "
read number2
if (( $number1 < $number2 ));
 then
    echo "Number1 is less than number2"
 else
    echo "Number1 is not less than number2"
fi

If the number1 is less than number2, it will return true expression. Otherwise, it will execute the false code block.

using less than sign option to compare numbers in bash

You can see in this picture that if you take number1=3 and number2=2 as input numbers, it shows “Number1 is not less than number2” as 3 is greater than 2. But if you take number1=4 and number2=8, it shows “Number1 is less than number2” as 4 is less than 8.

Using Equal to Operator “==”

An equal sign == checks whether the numbers are equal. Here’s a bash script for this:

#!/bin/bash
echo "Enter an input number1: "
read number1
echo "Enter an input number2: "
read number2
if ((number1 == number2)); then
    echo "Number1 is equal to number2"
else
    echo "Number1 is not equal to number2"
fi

Here, the equal operator == indicates the equality between the number1 and number2. If the number1 is equal to number2, it will execute if block expression “Number1 is equal to number2”. Contrarily, it will return the else code block.

using equal to sign option to compare numbers in bash

As you can see, after giving both input numbers as 5, it shows “Number1 is equal to number2”. While giving different input numbers 5 and 6, it shows “Number1 is not equal to number2”.

Using Not Equal To Sign “!=”

The not equal sign != inside the double brackets ((, tests if the two input numbers are not equal. Based on this condition, it gives true or false outcomes.

To check the inequality between the numbers, write [ $number1 != $number2 ] in the bash script. Here’s how:

#!/bin/bash
echo "Enter an input number1: "
read number1
echo "Enter an input number2: "
read number2
if [ $number1 != $number2 ];
 then
    echo "Number1 is not equal to number2"
 else
    echo "Number1 is equal to number2"
fi

When the number1 is not equal to number2, it prints the if block code. Otherwise, it executes the else code block “Number1 is equal to number2”.

using not equal to sign option to compare numbers in bash

Since both number is 3, it shows that “Number1 is equal to number2”. While putting different input numbers 2 and 3, it shows “Number1 is not equal to number2”.

Using Greater Or Equal To Sign “>=”

This greater than or equal operator executes the true output If the 1st input integer is greater than or equal to the 2nd one.

To compare numbers in bash, you can use the >= sign within the double brackets. Let’s take a look at how it works.

#!/bin/bash
echo "Enter an input number1: "
read number1
echo "Enter an input number2: "
read number2
if (( $number1 >= $number2 ));
 then
    echo "Number1 is greater than and equal to number2"
 else
    echo "Number1 is not greater than and equal to number2"
fi

This greater than and equal sign >= checks whether the number1 is greater than or equal to number2. If the condition is true, the script executes the if block expression. Otherwise, it executes the else block expression.

using greater than or equal to sign option to compare numbers in bash

Here, when both input numbers are 15, it shows “Number1 is greater than and equal to number2”. But after taking number1=15 which is less than the number2=16. In this case, it executes “Number1 is not greater than and equal to number2”.

Using Less Or Equal to Sign “<=”

The less than and equal sign <= works like the -le option discussed in the previous section. To compare numbers with <=, use (( $number1 <= $number2 )) to get the actual output. Here’s the bash script for this:

#!/bin/bash
echo "Enter an input number1: "
read number1
echo "Enter an input number2: "
read number2
if (( $number1 <= $number2 ));
 then
    echo "Number1 is less than and equal to number2"
 else
    echo "Number1 is not less than and equal to number2"
fi

If the condition is true, the script executes the if block expression. Otherwise, it executes the else block expression “Number1 is not less than and equal to number2”.

using less than and equal to sign option to compare numbers in bash

After executing the script and taking both input numbers as 25, it shows “Number1 is less than and equal to number2”. With the input number1=25 and number2=24, it displays “Number1 is not less than and equal to number2”.

Summary of Operators for Double Parenthesis

Comparison operators used with double parentheses in the bash script are as follows:

Comparison Operator Syntax Description
Greater than (>) (( $number1 > $number2 )) Returns true if the number1 is greater than number2.
Less than (<) (( $number1 < $number2 )) Returns true if the number1 is less than number2.
Equal to (==) (( $number1 == $number2 )) Returns true if the number1 is equal to number2.
Not equal to (!=) (( $number1 != $number2 )) Returns true if the number1 is not equal to number2.
Greater than or Equal to (>=) (( $number1 >= $number2 )) Returns true if the number1 is greater than or equal to number2.
Less than or Equal to (<=) (( $number1 <= $number2 )) Returns true if the number1 is less than and equal to number2.

Conclusion

In this article, I have showed two approaches, along with 12 different operators, for comparing integer numbers in the if else statement in bash. Certain operators demand double parentheses, whereas others require square brackets. To make sure you are using the right operator with the right bracket, review each step thoroughly. Good luck to you.

People Also Ask

How to compare strings in bash?

You can compare strings using comparison operators like greater than (>), equal to (=), not equal to (!=), and less than (<). In addition, you can apply -z and -n options to do this task.

Can I use ‘regex’ operator to compare numbers in bash?

No, you can not use the regular expression =~ to compare numbers in Bash. You can apply this operator within the if condition to compare strings.

How can I compare two floating point numbers in bash?

To compare floating numbers in bash, you can use the bc command in conjunction with the == operator inside the if statement. Check this bash script:

#!/bin/bash
num1=3.1415
num2=3.1416

# Use bc for floating-point comparison
if [ $(echo "$num1 == $num2" | bc -l) -eq 1 ]; then
    echo "Numbers are equal"
else
    echo "Numbers are not equal"
fi

This script uses the command bc -l to compare the numbers. If the result is equal to 1, then the numbers are the same. Otherwise, they’re not.

Can I compare three numbers in Linux?

Yes, definitely. You can use the logical AND (&&) operator inside the if condition to compare three numbers in Linux. To check whether the num1 is greater than the num2 and num3, use this syntax in your bash script: if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ].

Related Articles


<< Go Back to If Else in Bash | Bash Conditional Statements | Bash Scripting Tutorial

4.7/5 - (4 votes)
Mitu Akter Mou

Hello, This is Mitu Akter Mou, currently working as a Linux Content Developer Executive at SOFTEKO for the Linuxsimply project. I hold a bachelor's degree in Biomedical Engineering from Khulna University of Engineering & Technology (KUET). Experiencing new stuff and gathering insights from them seems very happening to me. My goal here is to simplify the life of Linux users by making creative articles, blogs, and video content for all of them. Read Full Bio

Leave a Comment