How to Use the "bc" Calculator in Scripts

man using computer
Thomas Barwick/Stone/Getty Images

The Linux program bc can be used 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, the Bash shell provides a few other methods for performing arithmetic operations.

Note: The bc program is also called basic calculator or bench calculator.

bc Command Syntax

The syntax for the bc command is similar to the C programming language, and a variety of operators are supported, like addition, subtraction, plus or minus, and more.

These are the various switches available with the bc command:

  • -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.

See this bc Command Manual for more details about how you can use the basic calculator.

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

When performing 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.

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.

In order to execute this script, open a terminal window and navigate to the directory where the script is located. We'll assume the script file is called bc_script.sh. Make sure the file is executable using the chmod command:

chmod 755 bc_script.sh

Then you would enter:

./bc_script.sh

The result would be the following:

2

In order to show 3 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

For better readability, the line with the calculations can be rewritten on multiple lines. In order the break the command line into multiple lines you can put a backslash at the end of the line:

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

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

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

The first command line argument is accessed using the variable "$1", the second argument uses "$2", etc.

Now you can write your own customized arithmetic functions in separate Bash scripts and call them from other scripts.

For example, if script1 contains:

#!/bin/bash
echo "scale=3; 
var1 = 6.5 / 2.7;
var2 = 14 * var1;
var2 *= $1;
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.