Software >> OS >> Unix >> Linux >> Shell >> How to perform arithmetic such as addition subtraction etc in bash and sh

Example, readh 2 numbers and find the sum, difference, product and quotient

bash

read X
read Y
echo $((X+Y))
echo $((X-Y))
echo $((X*Y))
echo $((X/Y))

sh

read X
read Y
echo `expr $X + $Y`
echo `expr $X - $Y`
expr `expr $X \* $Y`     (note that * is special i.e. wildcard character, hence need to be escaped with backslash)
expr `expr $X \/ $Y`     (note that / is special i.e. wildcard character, hence need to be escaped with backslash)