Bash Script – Arithmetic Operators
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: 16/3 result = 5 16/ 3 result = 1 x= 13 x+=3 result = 16 x= 13 x -= 3 result = 10 x= 10 x*=3 result = 30 x = 31 x/=3 result = 10 x= 31 x%=3 result = 1 3**2 result = 9Operator Name Use Example + Addition It adds two operands result= a+b – Subtraction It subtract second operand from first one result= a-b * Multiplication Multiply two operands result= a*b / Division Return the quotient after diving first operand from second operands % Modulo Return remainder after dividing first operand from second operand += Increment by constant Increment value of first operand with given constant value -= Decrement by constant Decrement value of first operand with given constant value *= Multiply by constant Multiply the given operandthe constant value /= Divide by constant Divide the operand with given constant value and return the quotient %= Remainder by dividing with constant Divide the operand with given constant value and return the remainder ** Exponentiation The result is second operand raised to the power of first opd.
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 codeCode:
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