Numeric vs String Comparison Operators
In this page:
Numeric Comparison Operators
Bash's numeric comparison operators -- -eq, -ne, -gt, -ge, -lt, -le -- work inside [ ], [[ ]], and treat their operands strictly as integers. They correctly compare 9 and 10 as numbers, unlike string comparison would.
Example: Numeric Comparison Operators
#!/bin/bash
a=9
b=10
if [ "$a" -lt "$b" ]; then
echo "$a is numerically less than $b"
fi
Login to try C/C++/Java/PHP code in the editor
String Comparison Operators
String comparison uses =/== for equality and </> (inside [[ ]], or escaped as \</\> inside [ ]) for lexical (dictionary/byte) ordering, comparing character by character rather than numeric value. This means "9" > "10" is true as strings, since 9 sorts after 1.
Warning: This looks wrong numerically but is correct string behavior -- use -gt/-lt when you mean numeric comparison.
Example: String Comparison Operators
#!/bin/bash
a="9"
b="10"
if [[ $a > $b ]]; then
echo "As strings, '$a' sorts after '$b'"
fi
Login to try C/C++/Java/PHP code in the editor
Why Leading Zeros and Widths Matter for Strings
Because string comparison works character by character, values with different lengths or leading zeros can sort unexpectedly compared to their numeric meaning. Numeric comparison operators avoid this entirely by converting both sides to actual integers first.
Example: Why Leading Zeros and Widths Matter for Strings
#!/bin/bash
id1="007"
id2="10"
if [[ $id1 < $id2 ]]; then
echo "As strings: '$id1' sorts before '$id2'"
fi
if [ "$id1" -lt "$id2" ]; then
echo "As numbers: $id1 is less than $id2"
fi
Login to try C/C++/Java/PHP code in the editor
Comparing Decimals with an External Tool
Bash's -eq/-lt/etc. and (( )) only support integers, so comparing decimal numbers like 3.5 requires an external tool such as bc or awk, since attempting [ 3.5 -gt 2 ] produces an 'integer expression expected' error. awk is commonly available and can perform the floating-point comparison directly.
Example: Comparing Decimals with an External Tool
#!/bin/bash
a="3.5"
b="2"
if awk -v x="$a" -v y="$b" 'BEGIN { exit !(x > y) }'; then
echo "$a is greater than $b (decimal-aware comparison)"
fi
Login to try C/C++/Java/PHP code in the editor
- Using
=or==to compare numbers as if they were interchangeable with-eq;"5" = "5.0"is false (different text), while5 -eq 5.0would actually error since arithmetic context requires plain integers, so the two systems aren't interchangeable at all. - Assuming
-gt,-lt, etc. work on decimal numbers; they only handle integers, so comparing"3.5" -gt "2"causes an error, not a numeric comparison. - Using string comparison operators on numbers that have leading zeros or different digit counts, expecting numeric ordering;
"9" > "10"is true as a *string* comparison (because 9 sorts after 1 character-by-character) even though it's false numerically.
- String comparison:
=/==(equal),!=(not equal),</>(lexical ordering, need escaping or[[ ]]). - Numeric comparison:
-eq,-ne,-gt,-ge,-lt,-le-- integers only, no decimals. - String
</>compare character by character using byte/locale ordering, not numeric magnitude. - For floating-point comparisons, Bash's built-in arithmetic isn't enough -- use an external tool like
bcorawk.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: