Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Text Processing

Linux bc command

Reference for the Linux bc calculator: decimals with scale, big integers, bases, the math library, variables and functions, and how to use bc from shell scripts.

bc is a calculator language with arbitrary precision. Bash can only do integer arithmetic and expr is limited the same way, so when a script needs decimals (10 / 4 as 2.50), very large integers, or a number in another base, bc is the standard answer. It reads expressions from standard input or a file, evaluates them, and prints each result on its own line.

Precision for division and similar operations is controlled by a variable named scale, the number of digits after the decimal point. It defaults to 0, which is the most common surprise: echo "10/4" | bc prints 2.

Terminal
echo "scale=2; 10/4" | bc

bc is not installed on the runner behind the live examples on this site, so the bc examples below are read-only. The runnable examples show the closest tools that are available there (awk and Bash arithmetic). On your own machine install it with sudo apt install bc or the equivalent.

Try it

Read-only: run these in your own terminal.

Terminal
echo "scale=2; 10/4" | bc          # 2.50
echo "2^100" | bc                   # 1267650600228229401496703205376
echo "scale=10; sqrt(2)" | bc       # 1.4142135623
echo "obase=2; 42" | bc             # 101010
echo "ibase=16; FF" | bc            # 255
echo "scale=20; 4*a(1)" | bc -l     # pi from arctangent: 3.14159265358979323846
Bash Doing the same job without bc

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
bc [-lqs] [FILE]...
Option Description
-l, --mathlib Load the math library and set scale to 20. Provides s(x) sine, c(x) cosine, a(x) arctangent, l(x) natural log, e(x) exponential, j(n,x) Bessel function.
-q, --quiet Do not print the welcome banner (the default in newer versions).
-s, --standard Reject non-POSIX extensions.
FILE Evaluate the file first, then read standard input.

Built-in variables:

Name Description
scale Digits kept after the decimal point (default 0).
ibase, obase Input and output number base (default 10).
last or . The most recent result (GNU extension).

Language features include + - * / % ^, comparison operators, if, while, for, define f(x) { ... } functions with return, arrays, length(x), scale(x) and sqrt(x). Quit with quit.

Returns: 0 on success. Errors in the input print a message and set a non-zero status only for fatal conditions.

Usage notes

  • Set scale first. Division, sqrt and % use it. scale=2; 1/3 gives .33, with no leading zero in GNU bc; format with printf '%.2f' or awk if you need 0.33.
  • scale does not round, it truncates. scale=2; 2/3 prints .66, not .67.
  • ibase must come after obase for hex digits. Setting ibase=16 first makes later values like obase=10 read as sixteen. Use obase=A style digits or set obase first.
  • Use -l for functions such as s() and l(). It also changes the default scale to 20.
  • Variables, not shell expansion. In echo "$a + $b" | bc, the shell fills in the values before bc runs. Quote the whole expression to keep spaces and * safe.
  • Comparisons print 1 or 0, so [ "$(echo "$a > $b" | bc)" -eq 1 ] tests floating-point values in a script. For long expressions prefer awk.
  • Large output lines are wrapped at 70 characters with a backslash. Set BC_LINE_LENGTH=0 to disable wrapping.
  • dc is the older reverse-Polish calculator that bc historically compiled to; use bc unless you already know dc.