Comparison Operators
In this page:
Comparison Operators
Inside [[ ]] or (( )), numeric comparisons use -eq -ne -lt -le -gt -ge (in [[ ]]) or the familiar == != < <= > >= (in (( ))). Bash keeps numeric comparison syntax separate from string comparison to avoid ambiguity. Mixing up -eq (numeric) with == in the wrong context is a common beginner mistake.
Warning: Using == inside [ ] (single brackets) works for strings, but for numbers you must use -eq, not ==.
Example: Comparison Operators
#!/bin/bash
a=5
b=8
if [ "$a" -lt "$b" ]; then
echo "$a is less than $b"
fi
if (( a < b )); then
echo "Using (( )): $a is less than $b"
fi
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: