Unix

In this tutorial we will learn about Arithmetic Operators in Shell Programming.

We will be covering the following math operations in this tutorial.

- Addition +
- Subtraction -
- Multiplication *
- Division /
- Modulus % to get remainder

In shell script all variables hold string value even if they are numbers. So, to perform arithmetic operations we use the `expr`

command.

The `expr`

command can only work with integer values. For floating point numbers we use the `bc`

command.

To compute the result we enclose the expression in backticks `` ``

.

We use the `+`

symbol to perform addition.

In the following example we are taking two integer value from the user and showing the result after addition.

```
#!/bin/sh
# take two integers from the user
echo "Enter two integers: "
read a b
# perform addition
result=`expr $a + $b`
# show result
echo "Result: $result"
```

In the above script ``expr $a + $b``

means we are adding values stored in variable `a`

and `b`

and evaluating the expression using the `expr`

command. We are then saving the result of the addition operation in the variable `result`

.

Output:

```
$ sh add.sh
Enter two integers:
10 20
Result: 30
```

In the following example we are enclosing the variables in double quotes and using `bc`

to handle floating point numbers.

```
#!/bin/sh
# take two numbers from the user
echo "Enter two numbers: "
read a b
# perform addition
result=`expr "$a + $b" | bc`
# show result
echo "Result: $result"
```

Output:

```
$ sh add2.sh
Enter two numbers:
1.2 3.4
Result: 4.6
```

To perform subtraction we use the `-`

symbol.

In the following example we will take two numbers from the user and print the subtraction result.

```
#!/bin/sh
# take two numbers from user
echo "Enter two numbers: "
read a b
# compute subtraction result
result=`expr "$a - $b" | bc`
# print output
echo "Result: $result"
```

Output:

```
$ sh subtract.sh
Enter two numbers:
10 9
Result: 1
$ sh subtract.sh
Enter two numbers:
9 10
Result: -1
$ sh subtract.sh
Enter two numbers:
10.5 9.1
Result: 1.4
```

To perform multiplication we use the `*`

symbol.

In the following example we will multiply two numbers.

```
#!/bin/sh
# take two numbers from user
echo "Enter two numbers: "
read a b
# compute multiplication result
result=`expr "$a * $b" | bc`
# print output
echo "Result: $result"
```

Output:

```
$ sh multiplication.sh
Enter two numbers:
2 3
Result: 6
$ sh multiplication.sh
Enter two numbers:
-2 3
Result: -6
$ sh multiplication.sh
Enter two numbers:
1.5 4
Result: 6.0
```

To perform division we use the `/`

symbol.

In the following example we will divide two numbers.

```
#!/bin/sh
# take two numbers from user
echo "Enter two numbers: "
read a b
# compute division result
result=`expr "$a / $b" | bc -l`
# print output
echo "Result: $result"
```

Output:

```
$ sh division.sh
Enter two numbers:
4 2
Result: 2
$ sh division.sh
Enter two numbers:
5 2
Result: 2.50000000000000000000
```

The `-l`

option loads the standard math library with default scale set to 20.

To perform modulus operations we use the `%`

symbol.

In the following example we will get the remainder by dividing two numbers.

```
#!/bin/sh
# take two numbers from user
echo "Enter two numbers: "
read a b
# compute modulus result
result=`expr "$a % $b" | bc`
# print output
echo "Result: $result"
```

Output:

```
$ sh modulus.sh
Enter two numbers:
5 2
Result: 1
$ sh modulus.sh
Enter two numbers:
5.1 2
Result: 1.1
```