BASH Programming

How Do I Compare Numbers in Bash?

You have learned about the many comparison operations that can be utilized to contrast variables in the bash. But sometimes, you might confront numerous situations in which you must develop scripts to carry out specific tasks and execute the script multiple times based on specific integers values in bash. In these situations, we can utilize the bash operators to make comparisons of integer numbers.

Different Ways of Comparing Integer Variables in Bash Scripting:

For integer numbers comparison, we will use two of the important operators in bash to get the comparisons which are square and double braces.

Way 01: Single Square Braces

The “[]” square braces are also referred to as single square braces to test the command in bash scripting. Let us include some examples that demonstrate the idea of bash scripting on Linux, so we can quickly implement the various operators of bash. Here are some examples of using different operators in single square braces.

Example 01: Use of “-eq” & “-ne” Operators

Let us put into practice the “-eq” operator, which determines whether several variables are equivalent in bash scripting as the initial demonstration of a comparison operator. To begin putting the example into practice, we must first create a bash scripting file by using the “touch” keyword in the file name and adding the “sh” extension. The entire file creation command is as follows:

linux@linux-VirtualBox:~$ touch compare.sh

When the aforementioned command is entered, a file called “compare.sh” will be produced. So that we may put the code into action, we then want to go into the “nano” editor. To accomplish this, we will add the “compare.sh” command after the “nano” keyword as shown below:

linux@linux-VirtualBox:~$ nano compare.sh

The nano editor will launch after entering is pressed. So that we can compare the two integer numbers, we have to write the necessary code in this section.

1: Equal To “-eq” Operator

We have used two variables in this code called “variable1” and “variable2” and we have used the “read” keyword which will get the values of these variables from the user on run time, respectively. The “if” statement was then used to determine whether the integer numbers are equal.

echo "Enter an input integer Variable1: "
read variable1
echo "Enter an input integer variable2: "
read variable2
if [ $variable1 -eq $variable2 ];
 then
    echo "Both input integer variables are equal...”
 else
    echo "
Both input integer variables are not equal...”
fi

To close the nano editor after finishing writing the program, type “ctrl + x.” Then, we returned to the main Linux terminal once more. Write “bash” and the file name to run the code.

linux@linux-VirtualBox:~$ bash compare.sh

The result of running the code shown above is seen here:

Enter an input integer variable1:
100
Enter an input integer variable2:
100
Both input integer variables are equal...

2: Not Equal To “-ne” Operator

Now, we will use the “-ne” comparison operator which means “not equal to”. Through this operator, we will check that “variable1” is not equal to “variable2” in the bash scripting. For that, we have done some modifications in the same above file which is “compare. sh”. In the modified code, we have just changed the values and the operator “-eq” with “-ne” as shown below:

echo "Enter an input integer Variable1: "
read variable1
echo "Enter an input integer variable2: "
read variable2
if [ $variable1 -ne $variable2 ];
 then
    echo "Both input integer variables are not equal...”
 else
    echo "
Both input integer variables are equal...”
fi

Here is the desired output shown in the image below:

Enter an input integer variable1:
50
Enter an input integer variable2:
100
Both input integer variables are not equal...

Example 02: Use of “-gt” & “-lt” Operators

This is another illustration of how to use the comparison operators “-gt” and “-lt” in the code. We will evaluate whether “variable1” is greater than or less than “Variable2” in the instance.

1: Greater Than “-gt” Operator

First, we determine whether variable 1 exceeds or does not exceed variable 2. The code snippet that demonstrates how the “-gt” operator is implemented is shown below.

echo "Enter an input integer Variable1: "
read variable1
echo "Enter an input integer variable2: "
read variable2
if [ $variable1 -gt $variable2 ];
 then
    echo “The variable1 is greater than variable2...”
 else
    echo "The variable1 is not greater than variable2...”
fi

When we executed the script, the result showed that “variable1″ was greater than ” variable2″.

Enter an input integer variable1:
98
Enter an input integer variable2:
96
The variable1 is greater than variable2...

2: Less Than “-lt” Operator

Now, using the “-lt” operator in bash scripting, we will determine whether ” variable1″ is less than ” variable2″ or not.

echo "Enter an input integer Variable1: "
read variable1
echo "Enter an input integer variable2: "
read variable2
if [ $variable1 -lt $variable2 ];
 then
    echo “The variable1 is less than variable2...”
 else
    echo "The variable1 is not less than variable2...”
fi

Consider the results of the example given above:

Enter an input integer variable1:
96
Enter an input integer variable2:
98
The variable1 is less than variable2...

Example 03: Use of “-ge” & “-le” Operator

In this example, we will check whether variable1 is greater than or less than variable2 and whether it might be equal to or not equal to variable2.

1: Greater Than or Equal To “-ge” Operator:

We are going to use the “-ge” operator to check if the “variable1” contains a greater value or equalized value to the “variable2”.

echo "Enter an input integer Variable1: "
read variable1
echo "Enter an input integer variable2: "
read variable2
if [ $variable1 -ge $variable2 ];
 then
    echo “The variable1 is greater than or equal to variable2...”
 else
    echo "The variable1 is not greater than or not equal to variable2...”
fi

By writing the “bash compare.sh”, we have the desired output:

Enter an input integer variable1:
25
Enter an input integer variable2:
24
The variable1 is greater than or equal to variable2...

2: Less Than or Equal To “-le” Operator

Now, we will employ the “-le” operator in the existing code so that we can check if the value of “variable1” is less than or equal to the “variable2” value.

echo "Enter an input integer Variable1: "
read variable1
echo "Enter an input integer variable2: "
read variable2
if [ $variable1 -le $variable2 ];
 then
    echo “The variable1 is less than or equal to variable2...”
 else
    echo "The variable1 is not less than or not equal to variable2...”
fi

Look at the output in the Linux terminal:

Enter an input integer variable1:
11
Enter an input integer variable2:
11
The variable1 is less than or equal to variable2...

Way 02: Double Square Braces

The “[]]” double braces do the same work as single square braces in bash. But in return, the bash will not return anything except 0 if the condition is true and return 1 if the condition is false. Now, we are going to implement different examples of the second way of using the comparison operator in the bash scripting.

Example 01: Use of (<) and (>) Operator

The following is a specified situation of a double square bracing in which we will apply the “>” and “<” indicators to determine if the value of “variable1” is greater than or less than the “variable2” parameter. First, we have created an entirely new file for the double square brackets technique for that:

linux@linux-VirtualBox:~$ touch double.sh

We have used the nano command to enter the nano editor:

linux@linux-VirtualBox:~$ nano double.sh

Our current editor is the nano editor. Let us begin by writing some code:

1: Less Than “<” Operator

Here is the example of less than, where we have two variables, and we will get the values from the user so we have used “read”. Then, we will perform the less than operation on these variables.

echo "Enter an input integer Variable1: "
read variable1
echo "Enter an input integer variable2: "
read variable2
if (( $variable1 < $variable2 ));
 then
    echo "The Condition is True"
    echo "Return 0"
 else
    echo "The Condition is False"
    echo "Return 1"
fi

To compile the script, we have used the “bash” command along with the file name:

linux@linux-VirtualBox:~$ bash double.sh

Here is the output:

Enter an input integer variable1:
76
Enter an input integer variable2:
102
The Condition is True
Return 0

2: Greater Than “>” Operator

Now, we will use the next operator, the symbol “>,” to determine whether or not the value “variable1” is greater or not. As you can see, in this case:

echo "Enter an input integer Variable1: "
read variable1
echo "Enter an input integer variable2: "
read variable2
if (( $variable1 > $variable2 ));
 then
    echo "The Condition is True"
    echo "Return 0"
 else
    echo "The Condition is False"
    echo "Return 1"
fi

Following the script’s execution in the Linux terminal, we obtained this output:

Enter an input integer variable1:
27
Enter an input integer variable2:
22
The Condition is True
Return 0

Example 02: Use of (<=) & (>=) Operators

Now that the second example has been implemented, we can use the bash script operator “>=” to determine whether the value of variable1 is less than or equal to the value of variable2.

1: Less Than or Equal To “<=” Operator

In this case, we will check the less or equal value in these variables and print the output.

echo "Enter an input integer Variable1: "
read variable1
echo "Enter an input integer variable2: "
read variable2
if (( $variable1 <= $variable2 ));
 then
    echo "The Condition is True"
    echo "Return 0"
 else
    echo "The Condition is False"
    echo "Return 1"
fi

The outcome of the aforementioned demonstration is displayed in the screenshot below:

Enter an input integer variable1:
65
Enter an input integer variable2:
65
The Condition is True
Return 0

2: Greater Than or Equal To “>=” Operator

In the example below, the “>=” operator from the bash scripting is used to verify whether “D1” and “D2” have values that are larger than or equal.

echo "Enter an input integer Variable1: "
read variable1
echo "Enter an input integer variable2: "
read variable2
if (( $variable1 >= $variable2 ));
 then
    echo "The Condition is True"
    echo "Return 0"
 else
    echo "The Condition is False"
    echo "Return 1"
fi

Following the example’s intended outcomes:

Enter an input integer variable1:
44
Enter an input integer variable2:
44
The Condition is True
Return 0

Conclusion

We have learned how to use the bash script to compare integer numbers. Additionally, we covered how to use several operators in a bash script. With thorough explanations, we have built numerous examples of these operators in bash scripting.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.