LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

bc

Arbitrary precision calculator language

TLDR

Calculate expression
$ echo ["2+2"] | bc
copy
Division with decimals
$ echo ["scale=2; 10/3"] | bc
copy
Run bc script
$ bc [script.bc]
copy
Math library
$ bc -l
copy
Evaluate an expression directly
$ bc -e "[scale=2; 100/3]"
copy
Interactive calculator
$ bc
copy

SYNOPSIS

bc [options] [file...]

DESCRIPTION

bc is an arbitrary precision calculator language. It supports interactive calculation, scripting, and mathematical operations with user-definable precision for decimal calculations.The tool has been a Unix standard for decades and is widely used in shell scripts for arithmetic.

PARAMETERS

-l, --mathlib

Load math library (includes functions like s, c, a, l, e, and sets scale to 20)
-s, --standard
POSIX standard mode; error on any non-POSIX extensions
-q, --quiet
Don't print the normal GNU bc welcome banner
-i, --interactive
Force interactive mode
-w, --warn
Warn about POSIX non-compliance
-e expression, --expression expression
Evaluate expression; multiple -e options are processed in order
-v, --version
Print version number and exit

SPECIAL VARIABLES

scale

Number of decimal places (default: 0)
ibase
Input base (default: 10)
obase
Output base (default: 10)

OPERATORS

- **+, -, *, /** - Basic arithmetic- % - Modulo- ^ - Exponentiation- ++, -- - Increment/decrement- ==, !=, <, > - Comparisons

FUNCTIONS

With -l flag (also sets scale to 20):- s(x) - Sine (x in radians)- c(x) - Cosine (x in radians)- a(x) - Arctangent (returns radians)- l(x) - Natural logarithm- e(x) - Exponential (e raised to x)- sqrt(x) - Square root (available without -l)

WORKFLOW

$ # Simple math
echo "2+2" | bc

# Floating point
echo "scale=4; 22/7" | bc

# Hex to decimal
echo "ibase=16; FF" | bc

# Square root
echo "sqrt(2)" | bc -l

# Multiple operations
bc <<< "
scale=2
x = 5
y = 3
x / y
"
copy

CAVEATS

Unusual syntax for programmers. Default integer division (set scale). Variables persist in interactive mode. Limited string handling. For modern scripts, consider awk or programming languages.

HISTORY

bc was developed at Bell Labs for Unix in 1975 by Robert Morris and Lorinda Cherry, based on the earlier dc calculator. The GNU version, which is the most widely used today, was written by Philip A. Nelson.

SEE ALSO

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

Copied to clipboard
Kai