Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Bash Script – Arithmetic Operators

  • Last Updated : 04 Jan, 2022

In this article, we will see arithmetic operators in bash script. Arithmetic operators is used to perform arithmetic operations.

Bash script supports 11 arithmetic operators. All the operators with their uses is given below:

OperatorNameUseExample
+AdditionIt adds two operandsresult= a+b
SubtractionIt subtract second operand from first oneresult= a-b
*MultiplicationMultiply two operandsresult= a*b
/DivisionReturn the quotient after diving first operand from second operands

16/3

result = 5

%ModuloReturn remainder after dividing first operand from second operand

16/ 3

result = 1

+=Increment by constantIncrement value of first operand with given constant value

x= 13

x+=3

result = 16

-=Decrement by constantDecrement value of first operand with given constant value

x= 13

x -= 3

result = 10

*=Multiply by constantMultiply the given operand

the constant value

x= 10

x*=3

result = 30

/=Divide by constantDivide the operand with given constant value and return the quotient

x = 31

x/=3

result = 10

%=Remainder by dividing with constantDivide the operand with given constant value and return the remainder

x= 31

x%=3

result = 1

**Exponentiation The result is second operand raised to the power of first op

d.

3**2

result = 9

Let’s see the examples for the uses of arithmetic operators:

Addition

Code:

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

Output:

Subtraction

Code:

Difference=$((10-3))  
echo "Difference = $Difference"  

Output:

Multiplication

Code:

Product=$((10*3))  
echo "Product = $Product"  

Output:

Division

Code:

Division=$((10/3))  
echo "Division = $Division"  

Output:

Modulo

Code:

Modulo=$((10%3))  
echo "Modulo = $Modulo"  

Output:

Exponentiation

Code:

Exponent=$((10**2))  
echo "Exponent = $Exponent"  

Output:

Example to show use of a

he operators in a single code

Code:

x=10
y=20
echo "x=10, y=5"  
echo "Addition of x and y"  
echo $(( $x + $y ))  
echo "Subtraction of x and y"  
echo $(( $x - $y ))  
echo "Multiplication of x and 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 10, then x= "  
(( x += 10 ))    
echo $x  
echo "Decrementing x by 15, then x= "  
(( x -= 15 ))  
echo $x  
echo "Multiply of x by 2, then x="  
(( x *= 2 ))  
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:

Different ways to compute Arithmetic Operations in Bash

There are some of the different ways to perform Arithmetic Operations.

1. Double Parenthesis

This could be used for arithmetic expansion. Let’s see an example to see the use of double quotes.

Code:

#!/bin/bash
 
first=10
second=3
 
echo $(( first + second ))   # addition
echo $(( $first + $second )) # this is also valid
 
echo $(( first - second ))   # subtraction
echo $(( $first - $second )) # this is also valid
 

 Output:

13
13
7
7

2. Using let command

let command is used to perform arithmetic operations. 

Example

code:

#!/bin/bash
 
x=10
y=3
 
let "z = $(( x * y ))"  # multiplication
echo $z
let z=$((x*y))
echo $z
 
let "z = $(( x / y ))"  # division
echo $z
let z=$((x/y))
echo $z

Output:

30
30
3
3

3. expr command with backticks

Arithmetic expansion could be done using backticks and expr.

Code:

a=10
b=3

# there must be spaces before/after the operator

sum=`expr $a + $b`
echo $sum

sub=`expr $a - $b`
echo $sub

mul=`expr $a \* $b`
echo $mul

div=`expr $a / $b`
echo $div

Output:

13
7
30
3
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!