How can I compare numbers in Bash?

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:

OperatorDescriptionExample
-eqEqual to[ $a -eq $b ]
-neNot equal to[ $a -ne $b ]
-ltLess than[ $a -lt $b ]
-leLess than or equal to[ $a -le $b ]
-gtGreater than[ $a -gt $b ]
-geGreater 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

  1. Whitespace: Always add spaces around operators and values inside [ ].
  • Bad: [$a -eq$b]
  • Good: [ $a -eq $b ]
  1. Floating-Point Numbers: Avoid using -eq, -lt, etc., with decimals. Use bc instead.
  2. 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

ScenarioMethodExample
Integer ComparisonsUse -eq, -lt, etc., in [ ][ $a -eq $b ]
Symbolic SyntaxUse (( )) with <, >, etc.(( a >= b ))
Floating-PointUse bcecho "$a > $b" | bc -l
RangesCombine 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.

Leave a Reply

Your email address will not be published. Required fields are marked *