On the previous article we have seen the basic concepts of shell scripting. As a part of basic operator we will see arithmetic operations using shell scripts.

Arithmetic operators are used to perform normal arithmetics/mathematical operations. As like other programs it performs basic mathematic functions. There are 7 arithmetic operators.

We will write program for every individual operators by passing command line arguments.

For input we will be using read

And we will use corresponding operators to perform the arithmetic operations.

We will use echo to print the output.

Binary operation used to add two operands.

Open a file as add.sh

$ vim add.sh

Paste the below code

#!/bin/sh

echo "Please enter the numbers:"

read num1

read num2

sum=$(($num1 + $num2))

echo "The sum of $num1 and $num2 is $sum."

echo "Please enter the numbers:"

read num1

read num2

sum=$(($num1 + $num2))

echo "The sum of $num1 and $num2 is $sum."

Run the file as below,

jhony@ljlinux:~$ ./add.sh

Please enter the numbers:

10

10

The sum of 10 and 10 is 20.

Please enter the numbers:

10

10

The sum of 10 and 10 is 20.

Binary operation used to subtract two operands.

Open a file as sub.sh

$ vim sub.sh

Paste the below code

#!/bin/sh

echo "Please enter the numbers:"

read num1

read num2

diff=$(($num1 - $num2))

echo "The difference of $num1 and $num2 is $diff."

echo "Please enter the numbers:"

read num1

read num2

diff=$(($num1 - $num2))

echo "The difference of $num1 and $num2 is $diff."

Run the file as below,

jhony@ljlinux:~$ ./sub.sh

Please enter the numbers:

10

5

The difference of 10 and 5 is 5.

Please enter the numbers:

10

5

The difference of 10 and 5 is 5.

Binary operation used to multiply two operands.

Open a file as mul.sh

$ vim sub.sh

Paste the below code

#!/bin/sh

echo "Please enter the numbers:"

read num1

read num2

mul=$(($num1 * $num2))

echo "Multiplication of $num1 and $num2 is $mul."

echo "Please enter the numbers:"

read num1

read num2

mul=$(($num1 * $num2))

echo "Multiplication of $num1 and $num2 is $mul."

Run the file as below,

jhony@ljlinux:~$ ./mul.sh

Please enter the numbers:

10

5

Multiplication of 10 and 5 is 50.

Please enter the numbers:

10

5

Multiplication of 10 and 5 is 50.

Binary operation used to divide two operands.

Open a file as div.sh

$ vim div.sh

Paste the below code

#!/bin/sh

echo "Please enter the numbers:"

read num1

read num2

div=$(($num1 / $num2))

echo "Divident of $num1 by $num2 is $div."

echo "Please enter the numbers:"

read num1

read num2

div=$(($num1 / $num2))

echo "Divident of $num1 by $num2 is $div."

Run the file as below,

jhony@ljlinux:~$ ./mul.sh

Please enter the numbers:

10

5

Divident of 10 by 5 is 2.

Please enter the numbers:

10

5

Divident of 10 by 5 is 2.

Binary operation used to find remainder of two operands.

Open a file as div.sh

$ vim div.sh

Paste the below code

#!/bin/sh

echo "Please enter the numbers:"

read num1

read num2

mod=$(($num1 % $num2))

echo "The remainder of $num1 by $num2 is $mod."

echo "Please enter the numbers:"

read num1

read num2

mod=$(($num1 % $num2))

echo "The remainder of $num1 by $num2 is $mod."

Run the file as below,

jhony@ljlinux:~$ ./mul.sh

Please enter the numbers:

10

3

The remainder of 10 by 3 is 1.

Please enter the numbers:

10

3

The remainder of 10 by 3 is 1.

Unary operator used to increase the value of operand by one.

Open a file as inc.sh

$ vim inc.sh

Paste the below code

#!/bin/sh

echo "Enter the number:"

read num1

out=$(($num1 + 1))

echo "The increment of $num1 is $out."

echo "Enter the number:"

read num1

out=$(($num1 + 1))

echo "The increment of $num1 is $out."

Run the file as below,

jhony@ljlinux:~$ ./inc.sh

Enter the number:

5

The increment of 5 is 6.

Enter the number:

5

The increment of 5 is 6.

Unary operator used to decrease the value of a operand by one

Open a file as dec.sh

$ vim dec.sh

Paste the below code

#!/bin/sh

echo "Enter the number:"

read num1

out=$(($num1 - 1))

echo "The increment of $num1 is $out."

echo "Enter the number:"

read num1

out=$(($num1 - 1))

echo "The increment of $num1 is $out."

Run the file as below,

jhony@ljlinux:~$ ./dec.sh

Enter the number:

5

The decrement of 5 is 4.

Enter the number:

5

The decrement of 5 is 4.

Thats it for Arithmetic operations, Feel free to ask if you have any questions.

© 2022 Copyright **How In Linux**. All Rights Reserved

## Comments