Bash Arithmetic Operators

Bash Arithmetic Operators – There are 11 arithmetic operators supported by Bash Shell.

In this tutorial, we will learn the syntax, description and examples for each of the arithmetic operators.

Addition

Computes addition of two numbers provided as operands.

</>
Copy
#!/bin/bash

x=$(( 15 + 8 ))
echo $x
</>
Copy
#!/bin/bash

x=`expr 15 + 8`
echo $x

Output

23

Subtraction

Computes subtraction of second operand from first.

</>
Copy
#!/bin/bash

x=$(( 15 - 8 ))
echo $x
</>
Copy
#!/bin/bash

x=`expr 15 - 8`
echo $x

Output

7

Multiplication

Computes the product of two operands provided.

</>
Copy
#!/bin/bash

x=$(( 15 * 8 ))
echo $x
</>
Copy
#!/bin/bash

x=`expr 15 * 8`
echo $x

Output

120

Division

Computes the division of first operand by second operand and returns quotient.

</>
Copy
#!/bin/bash

x=$(( 15 / 8 ))
echo $x
</>
Copy
#!/bin/bash

x=`expr 15 / 8`
echo $x

Output

1

Exponentiation

Computes the result of second operand raised to the power of first operand.

</>
Copy
#!/bin/bash

x=$(( 15 ** 8 ))
echo $x
</>
Copy
#!/bin/bash

x=`expr 15 ** 8`
echo $x

Output

2562890625

Modulo

Computes reminder when the first operand is divided by second operand.

</>
Copy
#!/bin/bash

x=$(( 15 % 8 ))
echo $x
</>
Copy
#!/bin/bash

x=`expr 15 % 8`
echo $x

Output

7

Increment Variable by Constant

The operator increments the value of first operand by the constant provided.

</>
Copy
#!/bin/bash

x=2
let "x += 5"
echo $x
</>
Copy
#!/bin/bash

x=2
(( x += 5 ))
echo $x

Output

23

Decrement Variable by Constant

The operator decrements the value of first operand by the constant provided.

</>
Copy
#!/bin/bash

x=2
let "x -= 5"
echo $x
</>
Copy
#!/bin/bash

x=2
(( x -= 5 ))
echo $x

Output

-3

Multiply Variable by Constant

The operator multiplies the value of first operand by the constant provided.

</>
Copy
#!/bin/bash

x=2
let "x *= 5"
echo $x
</>
Copy
#!/bin/bash

x=2
(( x *= 5 ))
echo $x

Output

10

Divide Variable by Constant

The operator computes (variable / constant) and stores the result back to variable.

</>
Copy
#!/bin/bash

x=12
let "x /= 5"
echo $x
</>
Copy
#!/bin/bash

x=12
(( x /= 5 ))
echo $x

Output

2

Remainder of Dividing Variable by Constant 

The operator computes (variable % constant) and stores the result back to variable.

</>
Copy
#!/bin/bash

x=12
let "x %= 5"
echo $x
</>
Copy
#!/bin/bash

x=12
(( x %= 5 ))
echo $x

Output

2

Different ways to compute Arithmetic Operations in Bash

Following are some of the variations you may adopt to perform Arithmetic Operations.

Double Parenthesis

Double parenthesis could be used for arithmetic expansion. Following is an example that demonstrates the usage of double parenthesis for arithmetic operations.

</>
Copy
#!/bin/bash

x=10
y=3

echo $(( x + y ))	# addition
echo $(( $x + $y ))	# also valid

echo $(( x - y ))	# subtraction
echo $(( $x - $y ))	# also valid

echo $(( x * y ))	# multiplication
echo $(( $x * $y ))	# also valid

echo $(( x / y ))	# division
echo $(( $x / $y ))	# also valid

echo $(( x ** y ))	# exponentiation
echo $(( $x ** $y ))	# also valid

echo $(( x % y ))	# modular division
echo $(( $x % $y ))	# also valid

(( x += 4 ))	# increment variable by constant
echo $x

(( x -= 4 ))	# decrement variable by constant 
echo $x

(( x *= 4 ))	# mulitply variable by constant
echo $x

(( x /= 4 ))	# divide variable by constant
echo $x

(( x %= 4 ))	# Remainder of Dividing Variable by Constant
echo $x


let construction

let command is used to carry out arithmetic operations. Following is an example :

</>
Copy
#!/bin/bash

x=10
y=3
z=0

let "z = $(( x + y ))"	# addition
let z=$((x+y))	# also valid without double quotes when no spaces in expression
echo $z

let "z = $(( x - y ))"	# subtraction
let z=$((x-y))
echo $z

let "z = $(( x * y ))"	# multiplication
let z=$((x*y))
echo $z

let "z = $(( x / y ))"	# division
let z=$((x/y))
echo $z

let "z = $(( x ** y ))"	# exponentiation
let z=$((x**y))
echo $z

let "z = $(( x % y ))"	# modular division
let z=$((x%y))
echo $z

let "x += 4"	# increment variable by constant
echo $x

let "x -= 4"	# decrement variable by constant 
echo $x

let "x *= 4"	# mulitply variable by constant
echo $x

let "x /= 4"	# divide variable by constant
echo $x

let "x %= 4"	# Remainder of Dividing Variable by Constant
echo $x

Backticks

Arithmetic expansion could be done using backticks and expr (all purpose expression evaluator). Following is an example :

</>
Copy
a=5
b=7
c=`expr $a + $b`
echo $c		# 12

Conclusion

In this Bash Tutorial, we have learnt Arithmetic Operators supported by Bash Shell with example for each.