First page Back Continue Last page Graphics

Numeric Operators

Most of the common C-style arithmetic operators (widely used; see also Java, C++, C#, and so on) are available in bash, so the integer operations + - * / % can be used, as well as += -= *= /= %= and both the ++ and -- operators (see Quigley pages 884 - 885 for the complete list or search for /^arithmetic\ evaluation in bash(1)).

Float operations must be done in the bc or another calculator, or gawk, since bash does not support float directly. Thus:

echo `echo "scale=2; 15/4" | bc`

Or, using variables:

declare y=15; declare z=4; declare m

m=$(echo "scale=2; $y/$z" | bc)

echo $m

Both print the bash string 3.75 as the result.