Utilizing double-parens
One other straightforward technique to do integer math is to make use of double parentheses as within the examples beneath. As with the expr command, multiplications and divisions are completed previous to additions and subtractions.
$ echo $((3 + 4))
7
$ echo $((10 * 5))
50
$ echo $((24 / 3))
8
$ echo $((10 * 5 + 1))
51
$ echo $((1 + 5 * 10))
51
$ echo $((1 + 10 / 5))
3
Remember that the double-parens technique solely helps integers.
$ echo $((10 / 2.5))
-bash: 10 / 2.5: syntax error
Utilizing the bc command
The bc command is used for extra precision calculations. Whereas it’s a language in itself, it’s continuously used for floating level mathematical operations as within the examples beneath.
$ echo "12+5" | bc
17
$ echo "10^2" | bc
100
$ echo "3 + 4" | bc
7
$ echo "scale=2; 10 / 3" | bc
3.33
$ echo 11.11 + 4.3 | bc
15.41
The dimensions argument means that you can set the decimal precision that you really want.
The bc command additionally helps extra superior math with the -l (math library) possibility that handles numbers as floating level objects:
$ echo 100/3 | bc
33
$ echo 100/3 | bc -l
33.33333333333333333333
Utilizing awk
The awk command is simple to make use of and works for each integer and floating-point math. Listed here are just a few examples:
