A floating-point arithmetic
In 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 7.50
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
.
Or:
$ bc ((83.12 + 32.13) * 37.3) 4298.82
Many things can be done with the bc
utility, such as all types of arithmetic operations including binary and unary operations; it has many defined mathematical functions. It has its own programming syntax.
You can get more information about the utility bc
at http://www.gnu.org/software/bc/.
Let's look at using awk
for a floating-point arithmetic:
$ result=`awk -v a=3.1 -v b=5.2 'BEGIN{printf...