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.
Output:
Variables can also be used:
Run it with:
Output:
Using declare -i¶
declare -i creates an integer variable. Assignments to the variable are automatically evaluated as arithmetic expressions.
Output:
With command-line arguments:
Example:
Output:
Arithmetic Expansion¶
Arithmetic expansion is one of the most common and readable ways to perform calculations in Bash.
The syntax is:
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:
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:
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.
Output:
The decimal portion is discarded.
Bash does not produce:
For floating-point calculations, use bc.
Modulus Operator¶
The % operator returns the remainder after division.
Output:
This is particularly useful for checking whether a number is even or odd.
Output:
Exponentiation¶
Bash supports exponentiation using **.
Output:
Another example:
Output:
Reading Numbers from User Input¶
The read command can be used to accept numbers from the user.
Example:
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:
Output:
Octal¶
Use 8#:
Output:
Hexadecimal¶
Use 16#:
Output:
The general syntax is:
For example:
Output:
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:
You can control the number of decimal places.
Output:
Floating-Point Arithmetic with bc¶
For calculations involving decimal values, bc is commonly used.
Check whether bc is installed:
A simple calculation:
Output:
To obtain decimal places, use scale:
Output:
More precision:
Output:
Building a Bash Floating-Point Calculator¶
#!/bin/bash
echo "Enter a calculation:"
read userinput
echo "Result:"
echo "scale=2; $userinput" | bc
Example:
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:
and:
Arithmetic expansion is generally easier to read and is commonly preferred in modern Bash scripts.
For example:
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. -
letcan evaluate arithmetic expressions but is less commonly preferred in new scripts. -
declare -imakes a variable an integer. -
Bash integer division discards the fractional portion.
-
%returns the remainder and is useful for even/odd checks. -
**performs exponentiation. -
printfcan format and round floating-point values. -
bcshould be used when decimal/floating-point calculations are required. -
Bash supports binary, octal, decimal, and hexadecimal arithmetic through base notation.