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.

A linux terminal.

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.

Dividing two integers with no remainder on the Bash command line

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.

Dividing two integers on the Bash command line with a fractional remainder. The remainder is never shown.

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.

Illustraing the issue with BAsh only supporting integer arithmetic by dividing two integers. The fractional remainder is discarded.

We get our expected answer of two.

Now lets divide six by seven.

Clearly, that will have a fractional answer.

The bc welcome message and prompt

Zero is obviously wrong.

Lets loop back and try, dividing 16 by 7.

We get an answer of two.

Three example calculations in bc, in interactive mode

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.

bc defaults to showing no decimal places. Pi is truncated to 3.

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.

Using scale to tell bc to show up to 7 decimal places in the results of calculations

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.

bc only displays the decimal places it needs. Setting scale to 10 will not force 10 decimal places to be used. If an answer requires fewer decimal places, they’re the only ones that are displayted.

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.

You can add multiple calculation to a line by separating them with semi-colons.

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.

You can change the setting of scale for each calculation, even calculations on the same command line

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.

bc launched with the -l option, showing pi calculated to 20 decimal places

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.

Calculating the sine, cosine, and arctan in bc in interactive mode

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.

Redirecting input into bc and bc -l

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.

Using echo to pipe input into bc and bc -l

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.

Wrapping input that includes spaces in quotation marks to pipe it into bc and bc -l

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.

Using chmod to make a script executable

Output from the pi.sh script showing pi calculated to three different precisions