How to Use the BC Calculator in Scripts

Calculate values from a shell script

The Linux program bc functions as a convenient desktop calculator or as a mathematical scripting language. It's as easy as calling the bc command through a terminal.

Besides the bc utility, most shells provide a few other methods for performing arithmetic operations.

BC Command Syntax

The syntax for the bc command is similar to the C programming language.

The command supports several different switches:

  • -h, --help: Prints this usage and exits.
  • -i, --interactive: Forces interactive mode.
  • -l, --mathlib: Uses the predefined math routines.
  • -q, --quiet: Doesn't print the initial banner.
  • -s, --standard: Non-standard bc constructs are errors.
  • -w, --warn: Warns about non-standard bc constructs.
  • -v, --version: Prints version information and exits.

Review the manpage for bc for a detailed breakdown of the command's usage.

BC Command Example

The basic calculator can be used in a terminal by simply entering bc, after which you can type regular math expressions like this:

4+3

to get a result like this:

7
Linux bc command

Using 'bc' in a Script

When you perform a series of calculations repeatedly, it makes sense to use the bc calculator as part of a script. The simplest form of such a script would look something like this:

#!/bin/bash
echo '6.5 / 2.7' | bc

The first line is just the path the executable that runs this script—in this case, the Bash environment.

The second line contains two commands. The echo command generates a string containing the mathematical expression contained in single quotes (6.5 divided by 2.7, in this example). The pipe operator (|) passes this string as an argument to the bc program. The output of the bc program is then displayed on the command line.

Extending the Performance of 'bc'

To show three decimal places, since the true answer is 2.407407..., use a scale statement inside the string delimited by the single quotes:

#!/bin/bash
echo 'scale=3; 6.5/2.7' | bc
Linux bc with scale

For better readability, the line with the calculations can break over several lines. Put a backslash at the end of the line:

echo 'scale=3; 
var1 = 6.5 / 2.7;
var1 ' \
| bc

Using Arguments with 'bc'

To include arguments in your bc calculations, change the single quotes into double quotes so that the command-line parameter symbols are interpreted by the shell:

echo "scale=3; 
var1 = 6.5 / 2.7;
var2 = 14 * var1;
var2 *= var1;
var2 " \
| bc

The first command line argument is accessed using the variable var1, the second argument uses var2, etc.

Linux bc with variables

For example, if script1 contains:

#!/bin/bash
echo "scale=3;
var1 = 6.5 / 2.7;
var2 = 14 * var1;
var2 *= var1;
var2 " \
| bc

and script2 contains:

#!/bin/bash 
var0="100"
echo "var0: $var0"
function fun1
{
echo "scale=3;
var1 = 10;
var2 = var1 * $var0;
var2 " \
| bc
}
fres=$(fun1)
echo "fres: "$fres
var10=$(./script1 $fres);
echo "var10: "$var10;

then executing script2 will invoke script1 using a variable $fres computed in script2 as parameter.

Linux bc with two scripts