Quick Links
Summary
The Linux Bash shell supports integer arithmetic only.
It can neither understand nor cope with floating point calculations.
The bc utility gives you precision floating point calculations interactively and in shell scripts.
Hannah Stryker / How-To Geek
We may never know what was truly behind the decision.
This is true on the command line and in Bash shell scripts.
Depending on your use case, this can be problematic or show-stopping.
Linux comes with two utility applications that allow you to perform floating point calculations.
One of these is dc.
Its a bit of a curio, operating as it does inreverse-Polish notation.
The other tool is bc.
It can be used interactively or as a command, and its the solution well discuss here.
Related:9 Bash Script Examples
The Problem
Lets get Bash to divide six by three.
We get our expected answer of two.
Now lets divide six by seven.
Clearly, that will have a fractional answer.
Zero is obviously wrong.
Lets loop back and try, dividing 16 by 7.
We get an answer of two.
Whats happening is the fractional part of the answer is being discarded, so the answer is truncated.
There was no fractional part in the first example, so we get the correct answer.
The second example had no integer element in the answer, only a fractional part.
Because the fractional part has been discarded the answer were shown is zero.
In the third example, 7 divides into 16 twice, with a fractional remainder.
Again, the remainder is discarded, and the result is truncated.
The bc system launches, announces its version number, and then waits for your input.
Typing a calculation and pressing Enter causes bc to evaluate the calculation and display the answer.
Lets try a calculation that will have a fractional component in the answer.
Thats not what we expected.
To make the true answer visible we need to tell bc how many decimal places to display.
We do this with the scale command.
Well ask for seven decimal places, and re-do our calculation.
Finally, were getting somewhere.
The scale setting remains in place until you change it.
Setting the number of decimal places tells bc themaximumnumber of places to display.
It doesnt get padded with meaningless zeroes.
The answers are displayed on per line as usual, in the order the calculations were listed.
you’re able to include the scale command in the list, too.
With the standard library loaded, you’re able to use these functions in your calculations.
Sine, cosine, and arctangent use radian values.
It processes your input and displays the answer in the terminal window.
To pipe input to bc, the input has to be the output of another process.
Its convenient to use echo for this.
We can also referenceBash variables in our calculations, including parameters to the script.
Heres our example script.
Copy this text into an editor, save it as pi.sh, then close your editor.
We use two variables, first_number and second_number to hold two numerical values.
We use those variables in the input that were piping into bc.
Before we can try our script, we need to make it executable with chmod.
Lets try our script with different command line values.
We get pi displayed to number of places we specify on the command line to our script.