Math in Bash
Bash supports a number of basic and more advanced arithmetic operations. These include addition, subtraction, multiplication, division, modulus, incrementing, decrementing, and exponentiation.
There are a few common ways to perform mathematical operations on numbers in bash. These include double parentheses, and the let and expr commands.
Basic Arithmetic in Bash
A simple way to perform arithmetic in bash is to use double parentheses $(()). These are used so that the variables are interpreted as integers rather than strings. For example:
#!/bin/bash
a=5
b=3
result=$((a + b))
echo "Result: $result"
Output:
Result: 8
In the above example, we first initialized two variables, ‘a’ and ‘b’. Then we created a third variable ‘result’ equal to the sum of ‘a’ and ‘b’.
This example demonstrated addition; the following shows how we can perform subtraction, multiplication, and division as well:
#!/bin/bash
a=5
b=3
sum=$((a + b))
echo "Sum: $sum"
difference=$((a - b))
echo "Difference: $difference"
product=$((a * b))
echo "Product: $product"
quotient=$((a / b))
echo "Quotient: $quotient"
Output:
Sum: 8
Difference: 2
Product: 15
Quotient: 1
Modulus
Bash supports the modulus operator %
, which finds the remainder of a division. For example, 10 divided by 3 (10/3) is equal to three, with a remainder of 1. The modulus of this operation is 1.
#!/bin/bash
result=$((10 % 3))
echo "Modulus: $modulus"
Output:
Modulus: 1
Increment and Decrement
Bash supports the increment ((++)
) and decrement ((--)
) operators. Increment (++) adds one to a value, and decrement (–) subtracts one.
We need to use two sets of parentheses in order for the operation to work properly. For example:
#!/bin/bash
a=5
((a++))
echo "Incremented: $a"
Output:
Incremented: 6
The value of ‘a’ is initially 5 but we can that it has been incremented to 6.
Let’s look at decrementing as well:
#!/bin/bash
a=5
((a--))
echo "Decremented: $a"
Output:
Decremented: 4
This time, ‘a’ gets decremented down 4.
Exponents
Bash supports exponentiation using the **
operator.
For example:
result=$((2**3))
echo "Result: $result"
Output:
Result: 8
This example calculates 23 = 2 x 2 x 2, which is 8.
Using the let Command
The let
command is a built-in command in Bash that allows you to perform arithmetic operations and assignments.
The syntax for using let
is:
let expression
We can use arithmetic expressions with let
, including addition (+
), subtraction (-
), multiplication (*
), division (/
), modulus (%
), and exponentiation (**
).
Let’s see an example:
#!/bin/bash
# Addition
let result=5+3
echo "Addition: $result"
# Subtraction
let result=result-2
echo "Subtraction: $result"
# Multiplication
let result=result*4
echo "Multiplication: $result"
# Division
let result=result/2
echo "Division: $result"
# Modulus
let result=result%3
echo "Modulus: $result"
# Exponentiation
let result=result**2
echo "Exponentiation: $result"
Output:
Addition: 8
Subtraction: 6
Multiplication: 24
Division: 12
Modulus: 0
Exponentiation: 144
Using the let
command, we can perform various arithmetic operations in Bash scripts. It’s a convenient way to manipulate numeric values and perform calculations within our scripts.
Using the expr Command
In Bash, we can perform arithmetic operations using the expr
command. expr
evaluates expressions and prints the result to stdout.
The basic syntax of expr
for arithmetic is:
expr ARG1 OPERATOR ARG2
ARG1
and ARG2
are the arguments to the arithmetic operation and OPERATOR
is the arithmetic operator (+
, -
, *
, /
, %
,)).
Let’s see an example:
#!/bin/bash
# Addition
result=$(expr 5 + 3)
echo "Addition: $result"
# Subtraction
result=$(expr $result - 2)
echo "Subtraction: $result"
# Multiplication
result=$(expr $result \* 4)
echo "Multiplication: $result"
# Division
result=$(expr $result / 2)
echo "Division: $result"
# Modulus
result=$(expr $result % 3)
echo "Modulus: $result"
# Exponentiation (not supported by expr, using bc)
result=$(echo "scale=2; $result ^ 2" | bc)
echo "Exponentiation: $result"
Output:
Addition: 8
Subtraction: 6
Multiplication: 24
Division: 12
Modulus: 0
Exponentiation: 144.00
Note the following points:
- For multiplication (
*
), you need to escape the asterisk with a backslash (\
) to prevent it from being interpreted as a wildcard character by the shell. expr
does not support floating-point arithmetic. For exponentiation, you can usebc
(an arbitrary precision calculator language) as shown in the example.
Floating-Point Math in Bash
You may have noticed that all of these examples used integers rather than floating-point (decimal) numbers.
Bash does not support floating-point arithmetic natively. However, it is still possible to work with floating point numbers using external tools like awk
or bc
. For example:
result=$(awk 'BEGIN {print 10.5 / 3}')
echo "Result: $result"
These features allow you to perform a wide range of mathematical and arithmetic operations in Bash scripts.