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

INSTALL

sudo apt install bc
copy
sudo dnf install bc
copy
sudo pacman -S bc
copy
sudo apk add bc
copy
sudo zypper install bc
copy
brew install bc
copy
nix profile install nixpkgs#bc
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)

Braincup
Open source brain training for math, memory and focus
Braincup mini-games
41 mini-games · Apache-2.0
No ads · No tracking
Play in browser
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid
276 stars
From the maker of Linux Command Library
Copied to clipboard
Braincup
Open source brain training for math, memory and focus. 41 mini-games, from mental arithmetic to Sudoku, N-Back and Solo Chess.
Apache-2.0 licensed · No ads · No tracking · No account
From the maker of Linux Command Library
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid