Floating-point arithmetic

In the Bash shell, we can only perform integer arithmetic. If we want to perform arithmetic involving a floating point or fractional values, then we will need to use various other utilities, such as awk, bc, and similar.

Let's see an example of using the utility called bc:

    $ echo "scale=2; 15 / 2" | bc

For using the bc utility, we need to configure a scale parameter. Scale is the number of significant digits to the right of the decimal point. We have told the bc utility to calculate 15 / 2, and then display the result with the scale of 2.

Another example is the following:

    $ bc
    ((83.12 + 32.13) * 37.3)

Many things can be done with the bc utility, such as all types of arithmetic operations including ...

Get Learning Linux Shell Scripting - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.