To compare numbers in Bash, you use specific operators for integer comparisons (Bash does not natively support floating-point comparisons without external tools like bc
). Below are detailed methods and examples for numerical comparisons in Bash scripting.
1. Integer Comparisons
Bash uses operators like -eq
, -lt
, -gt
, etc., inside [ ]
or [[ ]]
constructs.
Common Operators:
Operator | Description | Example |
---|---|---|
-eq | Equal to | [ $a -eq $b ] |
-ne | Not equal to | [ $a -ne $b ] |
-lt | Less than | [ $a -lt $b ] |
-le | Less than or equal to | [ $a -le $b ] |
-gt | Greater than | [ $a -gt $b ] |
-ge | Greater than or equal to | [ $a -ge $b ] |
Example 1: Basic Comparison
a=10
b=20
if [ $a -lt $b ]; then
echo "$a is less than $b"
else
echo "$a is not less than $b"
fi
Output:
10 is less than 20
2. Using Arithmetic Expansion (( ))
For a more intuitive syntax (similar to other programming languages), use (( ))
with symbols like <
, >
, ==
, etc.
Example 2: Compare with Symbols
a=15
b=15
if (( a == b )); then
echo "$a is equal to $b"
fi
Output:
15 is equal to 15
3. Floating-Point Comparisons
Bash does not natively handle floating-point numbers. Use bc
(basic calculator) to compare decimals.
Example 3: Compare Decimals with bc
a=3.14
b=2.71
# Use bc to evaluate the condition (1 for true, 0 for false)
if (( $(echo "$a > $b" | bc -l) )); then
echo "$a is greater than $b"
fi
Output:
3.14 is greater than 2.71
4. Comparing Multiple Conditions
Combine comparisons using logical operators &&
(AND) and ||
(OR).
Example 4: Check Range
num=25
if (( num >= 10 && num <= 50 )); then
echo "$num is between 10 and 50"
fi
Output:
25 is between 10 and 50
5. Case Statements for Comparisons
Use case
to check against patterns or ranges (works for integers).
Example 5: Check Number Range
read -p "Enter a number: " num
case $num in
[0-9]) echo "Single-digit number" ;;
1[0-9]) echo "Between 10 and 19" ;;
2[0-9]|30) echo "Between 20 and 30" ;;
*) echo "Out of range" ;;
esac
Output (if input is 25):
Between 20 and 30
Common Pitfalls
- Whitespace: Always add spaces around operators and values inside
[ ]
.
- Bad:
[$a -eq$b]
- Good:
[ $a -eq $b ]
- Floating-Point Numbers: Avoid using
-eq
,-lt
, etc., with decimals. Usebc
instead. - String vs. Number: Bash treats variables as strings by default. Ensure variables contain numerical values:
a="10"
if [[ $a =~ ^[0-9]+$ ]] && [ $a -gt 5 ]; then
echo "Valid number greater than 5"
fi
Summary of Methods
Scenario | Method | Example |
---|---|---|
Integer Comparisons | Use -eq , -lt , etc., in [ ] | [ $a -eq $b ] |
Symbolic Syntax | Use (( )) with < , > , etc. | (( a >= b )) |
Floating-Point | Use bc | echo "$a > $b" | bc -l |
Ranges | Combine with && /|| | (( num >= 10 && num <= 50 )) |
Full Example: Interactive Comparison
#!/bin/bash
read -p "Enter first number: " num1
read -p "Enter second number: " num2
if (( $(echo "$num1 > $num2" | bc -l) )); then
relation="greater than"
elif (( $(echo "$num1 < $num2" | bc -l) )); then
relation="less than"
else
relation="equal to"
fi
echo "$num1 is $relation $num2"
Output (if inputs are 3.14 and 2.71):
3.14 is greater than 2.71
By using these methods, you can reliably compare numbers in Bash scripts.