We can use the expr command for arithmetic operations. The expr command
is an external command; the binary of the expr command is stored in the folder called /usr/bin/expr.
Perform an addition operation as follows:
$ expr 40 + 2
42
Perform a subtraction operation as follows:
$ expr 42 - 2
40
Perform a division operation as follows:
$ expr 40 / 10
4
Perform a modulus (getting remainder) operation as follows:
$ expr 42 % 10
2
$ expr 4 * 10
expr: syntax error
With the expr command, we cannot use * for multiplication. We need to use *
for multiplication:
$ expr "4 * 10"
4 * 10
$ expr 4 * 10
40
We will write a simple script to add two numbers. Write the shell script, arithmetic_01.sh as follows:
!/bin/bash x=5 y=2 z=`expr $x + $y` echo $z
Test the script as follows...