In bash shell, a quick way to perform a math calculation is to use the four main math operators in a dollar sign follow by double parentheses grouping $(()

# $VAR plus $VAR2

$(($VAR1 + $VAR2))

# Multiple math operations

AVERAGE=$(( ($A + $B + $C)/3 ))

Just as long as you have $ (( something )), everything inside the double parentheses will be evaluated as a math operation using regular algebraic precedences.

One thing to remember is that division in this style will not handle decimal. It will round to the nearest integer.

$ (( 5/3 )) will be evaluated to 1

$ (( 1/3 )) will be zero

$ (( 10 / 3 )) will be 3

## No comments:

## Post a Comment

Help a friend, share your knowledge