Numeric comparisons are essential for decision-making in Bash scripts. Whether you’re comparing user input, performing calculations, or implementing conditional logic, understanding how to compare numbers effectively is crucial. In this blog post, we’ll explore the various methods and operators used for numeric comparisons in Bash.

Basic Numeric Comparison Operators

Bash provides several operators for comparing numbers:

  • -eq: Equal to
  • -ne: Not equal to
  • -lt: Less than
  • -le: Less than or equal to
  • -gt: Greater than
  • -ge: Greater than or equal to

Example:

1
2
3
4
5
6
7
8
#!/bin/bash

x=10
y=20

if [ $x -lt $y ]; then
echo "$x is less than $y"
fi

Using Double Parentheses

For more complex arithmetic comparisons, you can use double parentheses (( )). This syntax allows you to use familiar mathematical comparison operators:

  • ==: Equal to
  • !=: Not equal to
  • <: Less than
  • <=: Less than or equal to
  • >: Greater than
  • >=: Greater than or equal to

Example:

1
2
3
4
5
6
7
8
#!/bin/bash

a=15
b=20

if (( a < b )); then
echo "$a is less than $b"
fi

Comparing Floating-Point Numbers

Bash doesn’t natively support floating-point arithmetic. However, you can use external commands like bc for floating-point comparisons:

1
2
3
4
5
6
7
8
#!/bin/bash

x=3.14
y=3.15

if (( $(echo "$x < $y" | bc -l) )); then
echo "$x is less than $y"
fi

Comparing Multiple Conditions

You can combine multiple comparisons using logical operators:

  • &&: AND
  • ||: OR

Example:

1
2
3
4
5
6
7
8
9
10
#!/bin/bash

age=25
salary=50000

if (( age >= 18 && salary >= 30000 )); then
echo "Eligible for loan"
else
echo "Not eligible for loan"
fi

Using test Command

The test command (which is equivalent to [ ]) can also be used for numeric comparisons:

1
2
3
4
5
6
7
#!/bin/bash

num=42

if test $num -eq 42; then
echo "The number is 42"
fi

Arithmetic Expansion

You can perform arithmetic operations within comparisons using $(( )):

1
2
3
4
5
6
7
8
#!/bin/bash

x=5
y=3

if (( $((x + y)) > 7 )); then
echo "The sum of $x and $y is greater than 7"
fi

Comparing Ranges

To check if a number falls within a range:

1
2
3
4
5
6
7
8
9
#!/bin/bash

score=75

if (( score >= 0 && score <= 100 )); then
echo "Valid score"
else
echo "Invalid score"
fi

Case Statement for Numeric Ranges

You can use case statements with pattern matching for numeric ranges:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash

age=25

case $age in
[0-12])
echo "Child"
;;
[13-19])
echo "Teenager"
;;
[20-64])
echo "Adult"
;;
*)
echo "Senior"
;;
esac

Comparing Exit Status

Remember that in Bash, 0 is considered true and any non-zero value is false when used as an exit status:

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash

is_even() {
return $(( $1 % 2 ))
}

if is_even 4; then
echo "4 is even"
else
echo "4 is odd"
fi

Conclusion

Mastering numeric comparisons in Bash is crucial for writing effective scripts. Whether you’re using basic comparison operators, double parentheses, or external tools like bc for floating-point comparisons, understanding these techniques will help you create more robust and flexible Bash scripts.
Remember to always consider the nature of your data (integers vs. floating-point) and choose the appropriate comparison method. With practice, you’ll become proficient in handling various numeric comparison scenarios in your Bash scripts.