← Back to Bash Course | Chapter 3: Control Flow | Lesson 12 of 14

Numeric vs String Comparison Operators

Bash uses completely different operators for comparing numbers than for comparing text, and mixing them up gives wrong or confusing results.

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

bash
#!/bin/bash
a=9
b=10
if [ "$a" -lt "$b" ]; then
    echo "$a is numerically less than $b"
fi

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

bash
#!/bin/bash
a="9"
b="10"
if [[ $a > $b ]]; then
    echo "As strings, '$a' sorts after '$b'"
fi

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

bash
#!/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

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

bash
#!/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
Common Mistakes
  1. Using = or == to compare numbers as if they were interchangeable with -eq; "5" = "5.0" is false (different text), while 5 -eq 5.0 would actually error since arithmetic context requires plain integers, so the two systems aren't interchangeable at all.
  2. 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.
  3. 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.
Chapter Summary
  • 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 bc or awk.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.