Skip to content

16. Bash Arithmetic Operations

Overview

Bash supports basic arithmetic operations such as addition, subtraction, multiplication, division, modulus, and exponentiation. Arithmetic can be performed using let, declare -i, arithmetic expansion $(( )), or external tools such as bc for floating-point calculations.

Integer Arithmetic

Bash variables are normally treated as strings, but Bash provides several ways to perform integer calculations.

Using let

The let command evaluates arithmetic expressions.

#!/bin/bash

let RESULT=10+5

echo "Result: $RESULT"

Output:

Result: 15

Variables can also be used:

#!/bin/bash

let RESULT=$1+$2

echo "$1 + $2 = $RESULT"

Run it with:

chmod +x calculator.sh
./calculator.sh 88 12

Output:

88 + 12 = 100

Using declare -i

declare -i creates an integer variable. Assignments to the variable are automatically evaluated as arithmetic expressions.

#!/bin/bash

declare -i RESULT

RESULT=20+30

echo "Result: $RESULT"

Output:

Result: 50

With command-line arguments:

#!/bin/bash

declare -i RESULT

RESULT=$1+$2

echo "$1 + $2 = $RESULT"

Example:

./calculator.sh 40 25

Output:

40 + 25 = 65

Arithmetic Expansion

Arithmetic expansion is one of the most common and readable ways to perform calculations in Bash.

The syntax is:

$(( expression ))

Example:

#!/bin/bash

echo "Addition: $((10 + 5))"
echo "Subtraction: $((10 - 5))"
echo "Multiplication: $((10 * 5))"
echo "Division: $((10 / 5))"
echo "Modulus: $((10 % 3))"
echo "Power: $((2 ** 8))"

Output:

Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulus: 1
Power: 256

Arithmetic expansion can also use variables:

#!/bin/bash

a=20
b=6

echo "Addition: $((a + b))"
echo "Subtraction: $((a - b))"
echo "Multiplication: $((a * b))"
echo "Division: $((a / b))"
echo "Modulus: $((a % b))"

Output:

Addition: 26
Subtraction: 14
Multiplication: 120
Division: 3
Modulus: 2

Arithmetic Operators

Operator Operation Example Result
+ Addition 5 + 3 8
- Subtraction 5 - 3 2
* Multiplication 5 * 3 15
/ Division 6 / 3 2
% Modulus/remainder 7 % 3 1
** Exponentiation 2 ** 3 8

Important: Integer Division

Bash performs integer division when using normal arithmetic expansion.

echo $((10 / 3))

Output:

3

The decimal portion is discarded.

Bash does not produce:

3.3333

For floating-point calculations, use bc.

Modulus Operator

The % operator returns the remainder after division.

echo $((10 % 3))

Output:

1

This is particularly useful for checking whether a number is even or odd.

#!/bin/bash

num=10

if (( num % 2 == 0 )); then
    echo "Even number"
else
    echo "Odd number"
fi

Output:

Even number

Exponentiation

Bash supports exponentiation using **.

echo $((2 ** 8))

Output:

256

Another example:

base=3
power=4

echo $((base ** power))

Output:

81

Reading Numbers from User Input

The read command can be used to accept numbers from the user.

#!/bin/bash

echo "Enter two numbers:"
read num1 num2

result=$((num1 + num2))

echo "Result: $result"

Example:

Enter two numbers:
23 45
Result: 68

The same approach can be used for other operations:

#!/bin/bash

echo "Enter two numbers:"
read a b

echo "Addition:       $((a + b))"
echo "Subtraction:    $((a - b))"
echo "Multiplication: $((a * b))"
echo "Division:       $((a / b))"
echo "Remainder:      $((a % b))"

Number Base Conversion

Bash arithmetic can work with different number bases.

Binary

Use 2# before the number:

result=$((2#10001))

echo "$result"

Output:

17

Octal

Use 8#:

result=$((8#16))

echo "$result"

Output:

14

Hexadecimal

Use 16#:

result=$((16#E6A))

echo "$result"

Output:

3690

The general syntax is:

$((base#number))

For example:

echo $((2#1010))
echo $((8#20))
echo $((16#FF))

Output:

10
16
255

Rounding Floating-Point Numbers

Bash itself does not provide native floating-point arithmetic, but printf can be used for formatting and rounding.

#!/bin/bash

number=3.3446

echo "Original: $number"

rounded=$(printf "%.0f" "$number")

echo "Rounded: $rounded"

Output:

Original: 3.3446
Rounded: 3

You can control the number of decimal places.

number=3.14159265

printf "%.2f\n" "$number"
printf "%.4f\n" "$number"

Output:

3.14
3.1416

Floating-Point Arithmetic with bc

For calculations involving decimal values, bc is commonly used.

Check whether bc is installed:

which bc

A simple calculation:

echo "10 / 3" | bc

Output:

3

To obtain decimal places, use scale:

echo "scale=2; 10 / 3" | bc

Output:

3.33

More precision:

echo "scale=10; 10 / 3" | bc

Output:

3.3333333333

Building a Bash Floating-Point Calculator

#!/bin/bash

echo "Enter a calculation:"
read userinput

echo "Result:"
echo "scale=2; $userinput" | bc

Example:

Enter a calculation:
10/3.4
Result:
2.94

For more precision:

#!/bin/bash

echo "Enter a calculation:"
read userinput

echo "Result with 2 decimal places:"
echo "scale=2; $userinput" | bc

echo "Result with 10 decimal places:"
echo "scale=10; $userinput" | bc

Example:

Enter a calculation:
10/3.4

Result with 2 decimal places:
2.94

Result with 10 decimal places:
2.9411764705

let vs Arithmetic Expansion

Both approaches can perform integer calculations:

let result=10+20

and:

result=$((10 + 20))

Arithmetic expansion is generally easier to read and is commonly preferred in modern Bash scripts.

For example:

a=10
b=20

result=$((a + b))

echo "$result"

is straightforward and avoids the need for let.

Practical Example: Simple Calculator

#!/bin/bash

echo "Enter first number:"
read a

echo "Enter second number:"
read b

echo "Addition:       $((a + b))"
echo "Subtraction:    $((a - b))"
echo "Multiplication: $((a * b))"
echo "Division:       $((a / b))"
echo "Remainder:      $((a % b))"

Example:

Enter first number:
20
Enter second number:
6

Addition:       26
Subtraction:    14
Multiplication: 120
Division:       3
Remainder:      2

Key Takeaways

  • $(( )) is the standard way to perform integer arithmetic in Bash.

  • let can evaluate arithmetic expressions but is less commonly preferred in new scripts.

  • declare -i makes a variable an integer.

  • Bash integer division discards the fractional portion.

  • % returns the remainder and is useful for even/odd checks.

  • ** performs exponentiation.

  • printf can format and round floating-point values.

  • bc should be used when decimal/floating-point calculations are required.

  • Bash supports binary, octal, decimal, and hexadecimal arithmetic through base notation.