Bash Shell scripting – Arithmetic Expressions in BASH

The Arithmetic expression is very important feature for performing number arithmetic operations in scripts.
By default variables are treated as strings in bash scripts.
But parsing a string to numbers is very easy using double parentheses and external command such as expr.

Arithmetic operations in Bash Shell
Arithmetic operations can be done on integers using the following methods

$(( expression ))

Example:
Run the following commands on terminal:

Vim

1

2

3

4

5

6

7

$number_1=20

$number_2=5

$echo$((number_1+number_2))

$echo$((number_1-number_2))

$echo$((number_1*number_2))

$echo$((number_1/number_2))

$echo$((number_1%number_2))

You can also create a bash script for the above operations. For example we have a script operations.sh

Shell

1

2

3

4

5

6

7

8

9

#!/bin/bash

number_1=20

number_2=5

echo"Addition = $(( number_1 + number_2 ))"

echo"Subtraction = $(( number_1 - number_2 ))"

echo"Multiplication = $(( number_1 * number_2 ))"

echo"Division = $(( number_1 / number_2 ))"

echo"pre-increment = $(( ++number_1 ))"

echo"pre-decrement = $(( --number_2))"

now run the above script

Shell

1

2

$chmod+xoperations.sh

$./operations.sh

Output:

1

2

3

4

5

6

7

$./abc.sh

Addition=25

Subtraction=15

Multiplication=100

Division=4

pre-increment=21

pre-decrement=4

expr — evaluate expression
we can also use expr command to perform Arithmetic operations on numbers.
For example we have a script operations_expr.sh.