1. Overview

As Linux users, there will be times when we need to write a script to perform a certain task and iterate the script based on certain numeric conditions. In such cases, we use integer comparison operators in Bash. Specifically, there are two sets of integer comparison operators: square brackets and double parenthesis. Unfortunately, Bash doesn’t support floating point arithmetic.

In this tutorial, we’ll learn about these operators and how to compare variables with numbers in Bash.

2. Square Brackets

Square brackets can be used as either single square brackets or double square brackets.

Single square bracket, [, is another name for the test command. The test command is a built-in command of the Bash shell. It tests file attributes and performs string and arithmetic comparisons. Besides, the test command doesn’t provide an output, but it returns an exit status of 0 if the expression evaluates to true. Otherwise, it returns nonzero for false.

On the other hand, the double square bracket, [[, is a Bash extension inspired by the Korn shell. The double square bracket is a keyword. Keywords are special and operate under different sets of rules as commands. However, there are no general rules for keywords, and each keyword is a special case.

We should note that the [[  and ]] keywords don’t support path-name expansion or word splitting.

When Bash parses a script or a command on the terminal and notices a keyword that has a closing keyword, it detects everything between the two keywords. Then, Bash applies special treatment to them depending on the rules supported by the keywords. Furthermore, the square brackets keyword also supports shell globing.

Although single and square brackets differ, they have the same syntax for numeric comparison.

2.1. Check if Numbers Are Equal

We use the conditional expression -eq, meaning “equal to”, to check if numbers are equal. This compares the first and second operands to check if the operands are equal. After that, it returns true if both operands in comparison are equal:

#!/bin/bash
# Script for equal to numeric comparison
x=2
y=2
if [[ $x -eq $y ]]
   then
       echo "x and y are equal!"
   else
       echo "x and y are not equal!"
fi

Once we execute the script, the condition in the script evaluates to true and prints that x and y are equal.

2.2. Check if Numbers Are Not Equal

We also use the conditional expression, -ne, to see if two numbers are not equal. -ne is short for “not equal to”. In the same way, this expression compares the first and second operands to check if the operands aren’t equal to each other:

#!/bin/bash
# Script for not equal numeric comparison
x=2
y=3
if [[ $x -ne $y ]]
   then
       echo "x and y are not equal!"
   else
       echo "x and y are equal!"
fi

The expression evaluates to true since the values in the variables x and y are not equal to each other.

2.3. Check if Number Is Greater Than Other Number

Another conditional expression we have is -gt. This stands for “greater than”. -gt checks if the first operand is greater than the second operand. It returns true if the first operand is greater than the second. Otherwise, it returns false:

#!/bin/bash
# Script for greater than numeric comparison
x=3
y=1
if [[ $x -gt $y ]]
   then
       echo "x is greater than y!"
   else
       echo "x less than or equal to y!"
fi

Successfully executing the script prints out that x is greater than y because the expression evaluates to true.

2.4. More Operators

The following is a comprehensive list of conditional expressions for numeric comparison in both single and double square brackets:

Comparison Operator Syntax Explanation
is equal to [[ $x -eq $y ]] or [ $x -eq $y ] This returns true if both the integers in comparison are equal.
is not equal to [[ $x -ne $y ]] or [ $x -ne $y ] This evaluates to true if both the integers in comparison are not equal.
is greater than [[ $x -gt $y ]] or [ $x -gt $y ] This returns true if the integer value in the variable x is greater than the integer value in the variable y.
is less than [[ $x -lt $y ]] or [ $x -lt $y ] This returns true if the integer value in the variable x is less than the integer value in the variable y.
is greater than or equal to [[ $x -ge $y ]] or [ $x -ge $y ] This returns true if the integer value in the variable x is greater than or equal to the integer value in the variable y.
is less than or equal to [[ $x -le $y ]] or [ $x -le $y ] This returns true if the integer value in the variable x is less than or equal to the integer value in the variable y.

3. Double Parenthesis

Double parenthesis is a built-in arithmetic feature for the Bash shell. It is used to perform integer arithmetic operations.

Since double parenthesis is built-in, it’s more efficient. However, with the computing power, we have nowadays, the difference in performance is insignificant.

The syntax of double parenthesis is flexible. For instance, we include or take out the spacing inside the parenthesis, but it still works the same. In the case of variables, we don’t need to explicitly include the $ before the variable name as it doesn’t affect the expression.

Moreover, comparison expressions inside the parenthesis return either 1 for true or 0 for false.

3.1. Check if Numbers Are Equal

We use the operator, ==, to check if numbers inside the double parenthesis are equal. The operator compares the first operand and the second operand. It returns 1 for true and 0 for false:

#!/bin/bash
# Script for equal to numeric comparison
x=1
y=1
if (( $x == $y ))
   then
       echo "x is equal to y!"
   else
       echo "x is not equal to y!"
fi

The expression evaluates to true since the values in both variables, x, and y, are equal.

3.2. Check if Numbers Are Not Equal

In double parenthesis, we also use the != operator to compare numbers if they are not equal. The operator compares the left operand and the right operand. The expression evaluates to true or false:

#!/bin/bash
# Script for not equal to numeric comparison
x=2
y=1
if (( $x != $y ))
   then
       echo "x is not equal to y!"
   else
       echo "x is equal to y!"
fi

This evaluates to be true because the left and right operands are not equal.

3.3. More Operators

The following table outlines the list of integer comparison operators for double parenthesis:

Comparison Operator Syntax Explanation
is equal to (( $x == $y )) This returns true if both the integers in comparison are equal.
is not equal to (( $x != $y )) This evaluates to true if both the integers in comparison are not equal.
is greater than (( $x > $y )) This returns true if the integer value in the variable x is greater than the integer value in the variable y.
is less than (( $x < $y )) This returns true if the integer value in the variable x is less than the integer value in the variable y.
is greater than or equal to (( $x >= $y )) This returns true if the integer value in the variable x is greater than or equal to the integer value in the variable y.
is less than or equal to (( $x <= $y )) This returns true if the integer value in the variable x is less than or equal to the integer value in the variable y.

4. Conclusion

In this article, we learned about square brackets and double parenthesis comparison operators in Bash. We also looked at comparing integers in both cases.

Comments are closed on this article!