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" | bc7.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
.
Another example is the following:
$ 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 bc
utility at: http://www.gnu.org/software/bc/.
Let's look at using awk
for floating-point arithmetic:
$ result=`awk -v a...