Arithmetic Expansion Shell Scripting

Arithmetic Expansion - Shell Scripting

Arithmetic Expansion - Shell Scripting - 5.0 out of 5 based on 1 vote
User Rating:  / 1
PoorBest 

We have to use arthimetic expansion for doing any arithmetic calculations in shell script.

$(( expression ))

echo $(( 1 + 1 )) - will add 2 value

echo $(( 2 * 2 )) - multiply 2 value

echo $(( 2 / 2 )) - divide the one value by another

echo $(( 5 % 2 )) - modulo of 5 by 2

Note: shell does not support floating point so all the value will be rounded to the nearest number automatically

expr Command:

expr command is also used for calculating the arithmetic expressions

add=`expr 1 + 1`

mul=`expr 2 \* 6`

Note: in order to suppress * as regular expression \ is used before the *

div=`expr 6 / 2 `

modulo=`expr 7 % 2`

 

 

 

You are here: Home Tutorials Shell Scripting Arithmetic Expansion