Mathematical computatyion is vital part of scripts to calculate and estimate in scripts. `expr` tool is used to compute integer arithmetic, it doesnot work with floating number. it expects a space between every variable and operator. MAthematical operators such as '*' has already some meaning in linux so it neew '\' escape to get recognised as mathematical operator by shell.
" `` "(backtick) is way to get command inside backtick operated first than whole command. so, if expr is inside `` then expr will be calculated first before echo.
Ex : echo ` expr 2 + 4 `
Add : +
Subtract : -
Division : /
Multiply : \* (it need escape to not contradict to linux files * variable)
Modulus : %
There is one more way to caclculate integers by $(()) command. This calculates arithmetic operations in another shell in background and return to current shell. generally it is useful but it only access data provided and cannot access current variable as it is in another shell.
Ex : echo $(( $var1 + $var2 ))
Floating point Arithmetic :
Floating point is numbers with decimal values of in complex form other than integers. 'bc' tool is used to calculate the folating point arithmetic.
It takes whole expression as input. Expression can be passed to bc tool by piping.
Ex : Ex : echo `echo $a + $b | bc `
First echo is used to print to output , second echo is used to send expression to bc, bc uses pipes expression.