Integer Comparisons in Bash
Scripts constantly compare numbers: how many arguments were passed, how many lines a file has, how full a disk is, how many times a loop has run. Bash has no separate number type. Every variable holds text, and the shell decides whether to treat that text as a number only at the moment it is used. For that reason, comparing numbers needs its own set of operators, different from the string operators in lesson 70. This lesson covers the integer operators for [ ] and [[ ]], and the (( )) arithmetic command, which is the most natural way to write numeric conditions in Bash.
Understanding the Concept
An integer comparison converts both sides to whole numbers and compares their values. This is not the same as comparing characters. As text, "10" and "9" are compared character by character, and 1 comes before 9, so "10" sorts before "9". As numbers, ten is larger than nine. Choosing the wrong kind of operator is one of the most common sources of confusing results in shell scripts.
Bash supports whole numbers only. There are no decimals in Bash arithmetic, so a value such as 3.5 cannot be compared with these operators. For fractions, scripts call an external tool such as bc or awk.
The Six Integer Operators
Inside [ ] and [[ ]], integer comparisons use short word-like operators, which come from the names of the comparison:
| Operator | Meaning | Name |
|---|---|---|
-eq | equal | equal |
-ne | not equal | not equal |
-lt | less than | less than |
-le | less than or equal | less than or equal |
-gt | greater than | greater than |
-ge | greater than or equal | greater than or equal |
The symbols <, > and == are not used here, because inside [ ] the characters < and > are redirection symbols, and == compares strings.
Basic Example
a=10
b=20
[ "$a" -eq "$b" ]; echo "10 -eq 20: $?"
[ "$a" -ne "$b" ]; echo "10 -ne 20: $?"
[ "$a" -lt "$b" ]; echo "10 -lt 20: $?"
[ "$a" -le 10 ]; echo "10 -le 10: $?"
[ "$a" -gt "$b" ]; echo "10 -gt 20: $?"
[ "$a" -ge 10 ]; echo "10 -ge 10: $?"
Output:
10 -eq 20: 1
10 -ne 20: 0
10 -lt 20: 0
10 -le 10: 0
10 -gt 20: 1
10 -ge 10: 0
Understanding the Example
Each line runs one comparison and then prints $?, the exit status. As in lesson 66, status 0 means the condition is true and 1 means it is false. Ten is not equal to twenty, so -eq returns 1 and -ne returns 0. Ten is less than twenty, so -lt is true. It is not greater than twenty, so -gt is false. The last two lines show the inclusive forms: -le and -ge are both true when the two numbers are the same.
Every variable is quoted. That follows the rule from lesson 55, and for numbers it prevents a specific failure shown later in this lesson when a variable is empty.
Numbers Versus Strings
The same two values can give opposite answers depending on the operator:
[ 10 -gt 9 ]; echo "10 -gt 9 as numbers: $?"
[ "10" \> "9" ]; echo "10 > 9 as strings: $?"
Output:
10 -gt 9 as numbers: 0
10 > 9 as strings: 1
As numbers, ten is greater than nine. As strings, 10 comes before 9 in dictionary order, so