an arbitrary precision calculator language used in shell scripting. Use this tag for questions for programming-related uses of bc; general questions about bc usage and troubleshooting are a better fit at Unix & Linux Stack Exchange.
bc
, for basic calculator, is "an arbitrary-precision calculator language" using algebraic infix notation. bc
is typically used as either a mathematical scripting language or as an interactive mathematical shell.
Typical Usages
There are two ways bc
is typically used.
From a Unix command prompt, then interactively entering a mathematical expressions, such as
(1 + 3) * 2
, which will display8
.From the shell command prompt, where
bc
reads from standard input. For example$ echo '(1 + 3) * 2' | bc
will display8
in the shell.
While bc
can perform calculations to arbitrary precision, its default behavior is to truncate calculations to whole numbers. I.e. entering the expression 2/3
displays 0
. This can surprise new bc
users. The number of digits that bc
displays is determined by a variable scale
, which may be changed. For example, running bc
interactively, setting scale=7
then entering 2/3
will display .6666666
. Starting bc
with the -l
option loads a math library, setting scale
to 20 and loading these math functions
s (x) The sine of x, x is in radians.
c (x) The cosine of x, x is in radians.
a (x) The arctangent of x, arctangent returns radians.
l (x) The natural logarithm of x.
e (x) The exponential function of raising e to the value x.
j (n,x) The bessel function of integer order n of x.
In 1991, POSIX rigorously defined and standardized bc
. Two implementations of that standard survive today:
Traditional
bc
, on Unix and Plan 9 systems.GNU
bc
with numerous extensions beyond the POSIX standard, on Linux, etc.
Links & References
phodd.net/gnu-bc, a large collection of
bc
scriptsPOSIX bc specification from The Open Group Library
dc, the reverse-Polish calculator that
bc
is based on