How to Use the bc Calculator in Scripts

Calculate values from a shell script

BC Calculator script displayed on computer screen

 Pixabay / Mock Up Photos

The Linux bc program 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 other methods that perform arithmetic operations.

BC Command Syntax

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

The command supports several 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:


to get a result like this:

Linux bc command

Use 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:

echo '6.5 / 2.7' | bc

The first line is the path of 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.

Extend 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 single quotes:

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

Use Arguments with bc

To include arguments in bc calculations, change the single quotes to 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.

Linux bc with variables

For example, if script1 contains:

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

and script2 contains:

echo "var0: $var0"
function fun1
echo "scale=3;
var1 = 10;
var2 = var1 * $var0;
var2 " \
| bc
echo "fres: "$fres
var10=$(./script1 $fres);
echo "var10: "$var10;

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

Linux bc with two scripts
Was this page helpful?