Dec 312011
 

Sometimes in my small shell script i’ve to do some math, usually nothing too complex but it’s useful to do the math inside the bash script. Bash it’s really complete and among the many builtin functions there are also some mathematical functions.

Using the syntax $((expression)) you can evaluate the expression. Operations allowed are quite a few. From  man bash:

ARITHMETIC EVALUATION
The shell allows arithmetic expressions to be evaluated, under certain circumstances. Evaluation is done in fixed-width integers with no check for overflow, though division by 0 is trapped and flagged as an error. The operators and their precedence, asso ciativity, and values are the same as in the C language.
Continue reading »

Flattr this!