How to Evaluate Arithmetic Expressions in Bash?

Bash is a powerful shell scripting language used on Linux and Unix systems. One of the most common tasks in shell scripting is evaluating arithmetic expressions. This article explores various methods to evaluate mathematical calculations in Bash scripts.

Methods for Arithmetic Evaluation

Bash provides several ways to evaluate arithmetic expressions. While the traditional expr command exists, modern Bash offers more efficient built-in methods.

Using expr Command

The expr command evaluates expressions passed as arguments:

$ expr 2 + 3
5

Using Arithmetic Expansion $((...))

The preferred method uses double parentheses for arithmetic expansion:

$ echo $((2 + 3))
5
$ result=$((10 * 4))
$ echo $result
40

Using let Command

The let command performs arithmetic operations and stores results in variables:

$ let result=5+3
$ echo $result
8

Basic Arithmetic Operators

Bash supports all fundamental arithmetic operators for mathematical calculations:

Operator Symbol Example Result
Addition + $((7 + 3)) 10
Subtraction - $((10 - 4)) 6
Multiplication * $((6 * 7)) 42
Division / $((15 / 3)) 5
Modulus % $((17 % 5)) 2
Exponentiation ** $((2 ** 3)) 8

Examples

Example 1: Basic Operations

#!/bin/bash
a=10
b=3

echo "Addition: $((a + b))"
echo "Subtraction: $((a - b))"
echo "Multiplication: $((a * b))"
echo "Division: $((a / b))"
echo "Modulus: $((a % b))"
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1

Example 2: Order of Precedence

Bash follows standard mathematical precedence rules:

# Without parentheses: multiplication first
result1=$((2 + 3 * 4))
echo $result1

# With parentheses: addition first
result2=$(((2 + 3) * 4))
echo $result2
14
20

Example 3: Using Variables in Complex Expressions

#!/bin/bash
x=5
y=10
z=2

# Complex expression with variables
result=$((x * y + z ** 3 - 15 / 3))
echo "Result: $result"

# Increment operations
((x++))
echo "x after increment: $x"
Result: 53
x after increment: 6

Comparison with expr Command

Method Syntax Advantages Disadvantages
expr expr 2 + 3 Universal, works in all shells Requires escaping *, slower, external command
$(( )) $((2 + 3)) Built-in, faster, no escaping needed Bash-specific
let let result=2+3 Good for variable assignment Cannot be used in command substitution

Advanced Operations

Increment and Decrement

count=5
echo $((count++))  # Post-increment: prints 5, count becomes 6
echo $((++count))  # Pre-increment: count becomes 7, prints 7
echo $((count--))  # Post-decrement: prints 7, count becomes 6
echo $((--count))  # Pre-decrement: count becomes 5, prints 5

Conditional Expressions

a=10
b=20
max=$((a > b ? a : b))
echo "Maximum: $max"
Maximum: 20

Conclusion

Bash provides multiple methods for evaluating arithmetic expressions, with $(( )) being the most efficient and widely used approach. Understanding operator precedence and variable usage enables complex mathematical calculations in shell scripts. Modern Bash arithmetic expansion is faster and more convenient than the traditional expr command.

Updated on: 2026-03-17T09:01:38+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements