How to Use the BC Calculator in Scripts

Calculate from within Bash

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.

The bc program is also called a 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 get a result like this:

Linux bc command

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:

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 Make sure the file is executable using the chmod command:

chmod 755 

Then you would enter:


The result would be the following:

Linux bc with input piped

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:

echo 'scale=3; 6.5/2.7' | bc
Linux bc with scale

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 to 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 *= var1;
var2 " \
| bc

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

Linux bc with variables

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

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

Linux bc with two scripts