Bash Math Operations (Bash Arithmetic) Explained

Bash arithmetic refers to mathematical operations that can be performed within Bash scripts using special syntax. Bash provides built-in support for integer arithmetic operations, making it easy to perform calculations directly in shell scripts without external tools.

This article explores the fundamentals of Bash arithmetic, including syntax, operators, and practical examples for performing mathematical calculations in shell scripts.

Basic Syntax

Bash arithmetic uses the $(( )) syntax to evaluate mathematical expressions. The basic format is

$(( expression ))

The expression can include variables, numbers, and arithmetic operators. The double parentheses tell Bash to treat the content as an arithmetic expression rather than a regular string.

Arithmetic Operators

Bash supports several arithmetic operators for performing mathematical operations

Operator Description Example Result
+ Addition $(( 5 + 3 )) 8
- Subtraction $(( 10 - 4 )) 6
* Multiplication $(( 6 * 7 )) 42
/ Division (integer) $(( 15 / 3 )) 5
% Modulo (remainder) $(( 17 % 5 )) 2
** Exponentiation $(( 2 ** 4 )) 16
++ Increment $(( num++ )) num + 1
-- Decrement $(( num-- )) num - 1

Examples

Basic Operations

#!/bin/bash

# Basic arithmetic operations
echo "Addition: $(( 15 + 25 ))"
echo "Subtraction: $(( 50 - 20 ))"
echo "Multiplication: $(( 6 * 8 ))"
echo "Division: $(( 40 / 8 ))"
echo "Modulo: $(( 23 % 7 ))"
echo "Exponentiation: $(( 3 ** 3 ))"
Addition: 40
Subtraction: 30
Multiplication: 48
Division: 5
Modulo: 2
Exponentiation: 27

Using Variables

#!/bin/bash

num1=15
num2=8

sum=$(( num1 + num2 ))
product=$(( num1 * num2 ))
complex=$(( (num1 + num2) * 2 ))

echo "Sum: $sum"
echo "Product: $product"
echo "Complex: $complex"
Sum: 23
Product: 120
Complex: 46

Increment and Decrement

#!/bin/bash

counter=5
echo "Initial value: $counter"

echo "Pre-increment: $(( ++counter ))"
echo "After pre-increment: $counter"

echo "Post-increment: $(( counter++ ))"
echo "After post-increment: $counter"

echo "Pre-decrement: $(( --counter ))"
echo "After pre-decrement: $counter"
Initial value: 5
Pre-increment: 6
After pre-increment: 6
Post-increment: 6
After post-increment: 7
Pre-decrement: 6
After pre-decrement: 6

Floating-Point Operations with bc

Bash arithmetic performs integer division by default. For floating-point calculations, use the bc command

#!/bin/bash

# Integer division (default)
echo "Integer division: $(( 22 / 7 ))"

# Floating-point division using bc
echo "Floating-point division: $(echo "22/7" | bc -l)"
echo "With precision: $(echo "scale=2; 22/7" | bc -l)"
Integer division: 3
Floating-point division: 3.14285714285714285714
With precision: 3.14

Arithmetic in Conditional Statements

Bash arithmetic can be used in conditional statements and loops

#!/bin/bash

num=15

if (( num > 10 )); then
    echo "Number is greater than 10"
fi

if (( num % 2 == 0 )); then
    echo "Number is even"
else
    echo "Number is odd"
fi

# Loop with arithmetic
echo "First 5 squares:"
for (( i=1; i<=5; i++ )); do
    echo "$i squared is $(( i * i ))"
done
Number is greater than 10
Number is odd
First 5 squares:
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25

Key Points

  • Bash arithmetic works with integers only by default

  • Use $(( )) syntax for arithmetic expressions

  • Variables inside $(( )) don't need $ prefix

  • Division truncates to integers; use bc for decimals

  • Supports comparison operators in conditional statements

  • Pre/post increment and decrement behave like in C programming

Conclusion

Bash arithmetic provides a convenient way to perform mathematical calculations directly in shell scripts using the $(( )) syntax. While limited to integer operations, it handles most common scripting needs efficiently. For floating-point calculations, combine Bash with external tools like bc for more advanced mathematical operations.

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

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements