Bash

Bash Arithmetic Operators

Bash Arithmetic Operators

In this topic, we'll find out how to use arithmetic operators in Bash.

Depending on what sort of result we would like through our scripts, we may have to use arithmetic operators at some point. Like variables, they're reasonably easy to use. within the bash script, we will perform arithmetic operations on numeric values to urge the specified result.

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

Look at the subsequent table demonstrating the syntax, description and examples for every of the arithmetic operators:

  1. Addition (+)
  2. Subtraction (-)
  3. Multiplication (*)
  4. Division (/)
  5. Exponentiation (**)
  6. Modulo (%)
  7. Increment by constant (+=)
  8. Decrement by constant (-+)
  9. Multiply by constant (*=)
  10. Divide by Constant (/=)
  11. Remainder of Dividing by constant (%=)

The use of these operators will be shown using an script so that you can understand nicely and use them in your own code too.

There are many ways to perform arithmetic operations on the bash shell script. A number of the choices are given below that we will adopt to perform arithmetic operations:

Double Parentheses

Double parentheses are the easiest way to perform basic arithmetic operations within the Bash shell script. we will use this method by using double brackets with or without a number one $.

Syntax 

((expression))

The example:

#! /bin/bash 
 #in this script we will  learn about different ways to use double parenthesis in our scripts
 # 1st Way
 echo "Method 1"
 Sum=$((8+8))
 echo "sum $Sum"
#2nd Way
echo "Method 2"
 ((sum1=12+23))
echo "sum1 $sum1"
# 3rd and the 4th way is just an altered variant of 1st and 2nd method 
# in this we only use two variable and add them
# so lets see how its work
 
#3rd way 
echo "method 3"
var1=23
var2=45
Sum3=$((var1+var2))  
echo "varsum = $Sum3"
#4th way
echo "4th way"
var3=65
var4=65
((Sum2=var3+var4))
echo "varsum3 = $Sum2"

Output for the given code will be :


Text

Description automatically generated

Here is another script in which we have performed all the arithmetic operation

#!/bin/bash  
  
x=54
y=35  
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  

below is the output for the above program.
Text

Description automatically generated

Let Construction

In bash we have a in built way/ command to use or perform arithmetic operations.

Syntax:

let <arithmetic expression>

Here is the example of let command,

#!/bin/bash  
  
x=65 
y=8
z=0
echo "Addition"  
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  

The Output for the above program:
Text

Description automatically generated

Backticks

In bash scripting, an arithmetic expansion also can be performed using backticks and expr (known as all-purpose expression evaluator). The `expr` is analogous to 'let,' but it doesn't save the result to a variable. It directly prints the result. Unlike let, we do not got to enclose the expression within the quotes. We are required to use spaces between the things of the expression. it's important to notice that we should always use 'expr` within command substitution to save lots of the output to a variable.

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

Syntax

`expr item1 operator item2`  

or  

expr item1 operator item2  

Here is the example for Backticks,

#!/bin/bash  
#Basic arithmetic using expr  
  
 
echo "c is the value of addition c=a+b"  
a=45
b=54
echo $a
echo $b
echo "c= `expr $a + $b`"  

output of the above command,
Text, email

Description automatically generated