rakesh kumar

Posted on

# Bash Arithmetic Operators

Double Parentheses
((expression))

Sum=\$((10+3))

((Sum=10+3))

((Sum=Num1+Num2))

Sum=\$((Num1+Num2))

let "z = \$(( x ** y ))"

"c= `expr \$a + \$b`"

In this topic, we will understand how to use arithmetic operators in Bash.

Depending on what type of result we want through our scripts, we may need to apply arithmetic operators at some point. Like variables, they are reasonably easy to apply. In the bash script, we can perform arithmetic operations on numeric values to get the desired result.

There are 11 arithmetic operators which are supported by Bash Shell.

Look at the following table demonstrating the syntax, description and examples for each of the arithmetic operators:

Performing Arithmetic Operations in Bash
There are many options to perform arithmetic operations on the bash shell. Some of the options are given below that we can adopt to perform arithmetic operations:

Double Parentheses
Double parentheses is the easiest mechanism to perform basic arithmetic operations in the Bash shell. We can use this method by using double brackets with or without a leading \$.

Syntax

*((expression)) *
We can apply four different ways to achieve the required objectives. Look at the methods given below to understand how the double parentheses mechanism can be used (Assuming that we want to add the numbers 10 and 3) :

Method 1

``````Sum=\$((10+3))
echo "Sum = \$Sum"
``````

Method 2

``````((Sum=10+3))
echo "Sum = \$Sum"
``````

Method 3

``````Num1=10
Num2=3
((Sum=Num1+Num2))
echo "Sum = \$Sum"
``````

Method 4

``````Num1=10
Num2=3
Sum=\$((Num1+Num2))
echo "Sum = \$Sum"
``````

All of these methods will provide the same output as:

``````Sum = 13
``````

Following is an example demonstrating the use of double parentheses for arithmetic operations in a Bash shell script:

Bash Script

``````#!/bin/bash

x=8
y=2
echo "x=8, y=2"
echo "Addition of x & y"
echo \$(( \$x + \$y ))
echo "Subtraction of x & y"
echo \$(( \$x - \$y ))
echo "Multiplication of x & y"
echo \$(( \$x * \$y ))
echo "Division of x by y"
echo \$(( \$x / \$y ))
echo "Exponentiation of x,y"
echo \$(( \$x ** \$y ))
echo "Modular Division of x,y"
echo \$(( \$x % \$y ))
echo "Incrementing x by 5, then x= "
(( x += 5 ))
echo \$x
echo "Decrementing x by 5, then x= "
(( x -= 5 ))
echo \$x
echo "Multiply of x by 5, then x="
(( x *= 5 ))
echo \$x
echo "Dividing x by 5, x= "
(( x /= 5 ))
echo \$x
echo "Remainder of Dividing x by 5, x="
(( x %= 5 ))
echo \$x
``````

Output

Let Construction
Let is a built-in command of Bash that allows us to perform arithmetic operations. It follows the basic format:

Syntax

``````let <arithmetic expression>
``````

An example is given below explaining how to use let command in a Bash script:

Bash script

``````#!/bin/bash

x=10
y=6
z=0
let "z = \$(( x + y ))"
echo "z= \$z"

echo "Substraction"
let "z = \$((x - y ))"
echo "z= \$z"

echo "Multiplication"
let "z = \$(( x * y ))"
echo "z = \$z"

echo "Division"
let "z = \$(( x / y ))"
echo "z = \$z"

echo "Exponentiation"
let "z = \$(( x ** y ))"
echo "z = \$z"

echo "Modular Division"
let "z = \$(( x % y ))"
echo "z = \$z"

let "x += 5"
echo "Incrementing x by 5, then x= "
echo \$x

let "x -= 5"
echo "Decrementing x by 5, then x= "
echo \$x

let "x *=5"
echo "Multiply of x by 5, then x="
echo \$x

let "x /= 5"
echo "Dividing x by 5, x= "
echo \$x

let "x %= 5"
echo "Remainder of Dividing x by 5, x="
echo \$x
``````

Output

Backticks
In bash scripting, an arithmetic expansion can also be performed using backticks and expr (known as all-purpose expression evaluator). The `expr` is similar to 'let,' but it does not save the result to a variable. It directly prints the result. Unlike let, we don't need to enclose the expression within the quotes. We are required to use spaces between the items of the expression. It is important to note that we should use 'expr` within command substitution to save the output to a variable.

We can also use `expr` without 'backticks'.

Syntax

``` ```expr item1 operator item2``` or expr item1 operator item2 ```

An example is given below explaining how to use backticks and expr in a Bash script:

Bash Script Program

`

# Basic arithmetic using expr

echo "a=10, b=3"

echo "c is the value of addition c=a+b"

a=10

b=3

echo "c= `expr \$a + \$b`"
`

Output