LinuxCommandLibrary

bc

Perform arbitrary-precision arithmetic calculations

TLDR

Start an interactive session

$ bc
copy

Start an interactive session with the standard math library enabled
$ bc [[-i|--interactive]] [[-l|--mathlib]]
copy

Calculate an expression
$ echo '[5 / 3]' | bc
copy

Execute a script
$ bc [path/to/script.bc]
copy

Calculate an expression with the specified scale
$ echo 'scale = [10]; [5 / 3]' | bc
copy

Calculate a sine/cosine/arctangent/natural logarithm/exponential function using mathlib
$ echo '[s|c|a|l|e]([1])' | bc [[-l|--mathlib]]
copy

Execute an inline factorial script
$ echo "define factorial(n) { if (n <= 1) return 1; return n*factorial(n-1); }; factorial([10])" | bc
copy

SYNOPSIS

bc [OPTIONS] [FILE...]

Examples:
bc
bc -l
bc myfile.bc
echo "scale=4; 10/3" | bc

PARAMETERS

-l
    Loads the standard math library, which provides functions like sine, cosine, arctangent, natural logarithm, exponential, and square root.

-s
    Enables the POSIX bc syntax and disables GNU bc extensions. This ensures strict POSIX compliance.

-q
    Suppresses the normal GNU bc welcome message and copyright notice upon startup.

-w
    Prints warnings for GNU bc extensions when used with POSIX bc syntax.

-v, --version
    Displays the version information for bc and then exits.

--help
    Displays a help message with usage information and then exits.

[file ...]
    One or more files containing bc commands to be executed. bc processes these files in order, then reads from standard input if no -q option is used and standard input is not a terminal.

DESCRIPTION

bc stands for "basic calculator" and is a command-line utility and a programming language for arbitrary precision arithmetic. It can be used interactively or to execute scripts from files. It supports basic arithmetic operations (addition, subtraction, multiplication, division, modulo, exponentiation), variables, arrays, control flow statements (if, while, for), and user-defined functions.

Unlike typical shell arithmetic, bc automatically handles numbers of virtually any size and precision, limited only by available memory. This makes it invaluable for financial calculations, cryptographic operations, or scientific computations where floating-point inaccuracies are unacceptable. Input is typically read from standard input or specified files, and results are printed to standard output. Its behavior can be extended via the built-in math library or custom functions. It's often used in shell scripts for complex calculations that require higher precision than standard shell built-ins provide.

CAVEATS

Precision Control: The default precision (number of digits after the decimal point) is 0. Users must explicitly set the scale variable (e.g., scale=20) to achieve desired precision for division and square root operations.
GNU vs. POSIX: There are differences between GNU bc (commonly found on Linux) and POSIX bc. GNU bc includes extensions like the read function and a specific format for the for statement that are not present in POSIX bc.
Input Limitations: While bc handles large numbers, very complex or extremely long calculations might consume significant memory and processing time.

THE 'SCALE' VARIABLE

The scale variable is crucial for controlling the number of digits after the decimal point used in calculations. Its default value is 0. For example, to get '3.333' from '10/3', you must set scale=3 before the division. This variable affects division, square root, and other functions from the math library.

MATH LIBRARY (-L)

When invoked with the -l option, bc loads a standard math library. This library provides common mathematical functions such as s(x) for sine, c(x) for cosine, a(x) for arctangent, l(x) for natural logarithm, e(x) for exponential, and j(n,x) for Bessel functions. These functions operate on angles in radians.

HISTORY

bc originated in the early days of Unix at Bell Labs, developed by Robert Morris. It was designed as a front-end processor to the arbitrary precision reverse Polish notation calculator dc(1). While dc provided the core arbitrary precision capabilities, its RPN syntax was less intuitive for many users. bc was created to provide a more traditional infix notation interface, making arbitrary precision calculations more accessible.
The GNU project later developed its own version of bc, which is the most commonly encountered version on modern Linux systems. GNU bc extended the original POSIX specification with additional features and functions, providing more robust programming capabilities.

SEE ALSO

dc(1), awk(1), expr(1), calc(1)

Copied to clipboard