Questions tagged [bc]

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 display 8.

  • From the shell command prompt, where bc reads from standard input. For example $ echo '(1 + 3) * 2' | bc will display 8 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:

  1. Traditional bc, on Unix and Plan 9 systems.

  2. GNU bc with numerous extensions beyond the POSIX standard, on Linux, etc.

Links & References

281 questions
83
votes
14 answers

How do I get bc(1) to print the leading zero?

I do something like the following in a Makefile: echo "0.1 + 0.1" | bc (in the real file the numbers are dynamic, of course) It prints .2 but I want it to print 0.2. I would like to do this without resorting to sed but I can't seem to find how to…
rwos
  • 1,751
  • 1
  • 15
  • 18
67
votes
8 answers

How to calculate the log of a number using bc?

I want to calculate the log (base 10) of a number. How can I do this using bc?
Bruce
  • 33,927
  • 76
  • 174
  • 262
63
votes
5 answers

Floating point results in Bash integer division

I have a backup script on my server which does cron jobs of backups, and sends me a summary of files backed up, including the size of the new backup file. As part of the script, I'd like to divide the final size of the file by (1024^3) to get the…
Joel G Mathew
  • 7,561
  • 15
  • 54
  • 86
50
votes
11 answers

How to get bc to handle numbers in scientific (aka exponential) notation?

bc doesn't like numbers expressed in scientific notation (aka exponential notation). $ echo "3.1e1*2" | bc -l (standard_in) 1: parse error but I need to use it to handle a few records that are expressed in this notation. Is there a way to get bc to…
Ferdinando Randisi
  • 4,068
  • 6
  • 32
  • 43
34
votes
2 answers

bc is ignoring scale option

I can't figure out why bc tool sometimes ignores the scale option. Here is an example: > echo 'scale=2; 2.777 - 1.4744' | bc 1.3026 Expected result is: 1.30 Additional information: > bash --version GNU bash, version 2.05b.0(1)-release…
dabest1
  • 2,347
  • 6
  • 25
  • 25
32
votes
1 answer

How to get a decimal number when dividing in bc?

I need 'bc' to divide a number and give me not only the floor but also the remainder. For instance 'bc' gives me '2' if I do '5/2'. I'd really want something like '2.5' Maybe this isn't even possible?
EhevuTov
  • 20,205
  • 16
  • 66
  • 71
29
votes
6 answers

bc truncate floating point number

How do I truncate a floating point number using bc e.g if I do echo '4.2-1.3' | bc which outputs 2.9 how I get it to truncate/use floor to get 2
Bilal Syed Hussain
  • 8,664
  • 11
  • 38
  • 44
29
votes
1 answer

How to assign an output to a shellscript variable?

How to assign this result to a shell variable? Input: echo '1+1' | bc -l Output: 2 Attempts: (didn't work) #!bin/sh a=echo '1+1' | bc -l echo $a
GarouDan
  • 3,743
  • 9
  • 49
  • 75
24
votes
2 answers

Using fractional exponent with bc

bc, a Linux command-line calculator, is proficient enough to calculate 3^2 9 Even a negative exponent doesn't confuse it: 3^-2 0.11111 Yet it fails when it encounters 9^0.5 Runtime warning (func=(main), adr=8): non-zero scale in exponent How…
Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111
19
votes
4 answers

How can I calculate pi using Bash command

I am learning bash scripting. While exploring the math functions i am came across a command which calculated the value of pi. seq -f '4/%g' 1 2 99999 | paste -sd-+ | bc -l Although i understand how the basic seq command works, I am unable to…
poorvank
  • 7,524
  • 19
  • 60
  • 102
19
votes
4 answers

Hex to Binary conversion in bash

I'm trying to convert a series of bytes from hex to bin using bash. but I keep getting (seemingly random) "(standard_in) 1: syntax error" replies from the following code: for j in c4 97 91 8c 85 87 c4 90 8c 8d 9a 83 81 do BIN=$(echo…
fragman
  • 259
  • 1
  • 3
  • 9
17
votes
2 answers

Is there an `rc` file for the command line calculator `bc`?

Based on the answer to this question, I would like to default scale = 2 every time I start bc from the command line (or from inside vim). However, man bc did not mention a .bcrc file, and when I experimentally created ~/.bcrc, it did not have any…
merlin2011
  • 71,677
  • 44
  • 195
  • 329
16
votes
2 answers

Making decimal.Decimal the default numerical type in Python

Is there some way I could make decimal.Decimal the default type for all numerical values in Python? I would like to be able to use Python in a manner similar to the bc and dc programs without having to call decimal.Decimal(...) for every…
Eric Pruitt
  • 1,825
  • 3
  • 21
  • 34
13
votes
5 answers

bash, bc modulo does not work with -l flag

So I am trying to use bc to calculate some logarithms but I also need to use it to calculate the modulus for something. Whilst making my script, I launched bc to test it. Without any flags, bc <<< "3%5" of course returns 3. But with bc -l (loads…
Saad Attieh
  • 1,396
  • 3
  • 21
  • 42
13
votes
3 answers

Bash Scripting and bc

I'm trying to write a bash script and I needed to do some floating point math. Basically I want to do something like this: NUM=$(echo "scale=25;$1/10" | bc) if [ $? -ne 0 ] then echo bad fi The problem I'm running into is $? tends to hold the…
LandonSchropp
  • 10,084
  • 22
  • 86
  • 149
1
2 3
18 19