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.
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.
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
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
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
scalefirst. Division,sqrtand%use it.scale=2; 1/3gives.33, with no leading zero in GNUbc; format withprintf '%.2f'orawkif you need0.33. scaledoes not round, it truncates.scale=2; 2/3prints.66, not.67.ibasemust come afterobasefor hex digits. Settingibase=16first makes later values likeobase=10read as sixteen. Useobase=Astyle digits or setobasefirst.- Use
-lfor functions such ass()andl(). It also changes the default scale to 20. - Variables, not shell expansion. In
echo "$a + $b" | bc, the shell fills in the values beforebcruns. 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 preferawk. - Large output lines are wrapped at 70 characters with a backslash. Set
BC_LINE_LENGTH=0to disable wrapping. dcis the older reverse-Polish calculator thatbchistorically compiled to; usebcunless you already knowdc.
Related
expr,awk,printf- Lessons: Arithmetic, printf and date