Questions tagged [bc]

A good command line calculator program

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

15 questions
118
votes
5 answers

bc: set number of digits after decimal point

bc handles numbers as integers: # echo "100/3" | bc 33 bc -l handles numbers as floating point objects: # echo "100/3" | bc -l 33.33333333333333333333 Is there a way to limit the number of digits after the decimal point?
Adam Matan
  • 12,919
61
votes
16 answers

How to round decimals using bc in bash?

A quick example of what I want using bash scripting: #!/bin/bash echo "Insert the price you want to calculate:" read float echo "This is the price without taxes:" echo "scale=2; $float/1.18" |bc -l read -p "Press any key to continue..." bash…
blackedx
  • 678
19
votes
2 answers

How to pass results of bc to a variable

I'm writing a script and I would like to pass the results from bc into a variable. I've declared 2 variables (var1 and var2) and have given them values. In my script I want to pass the results from bc into another variable say var3 so that I can…
shaolin
  • 303
15
votes
5 answers

How to set default scale for bc calculator?

Ubuntu 14.04.1 LTS How do I set the default scale for the bc calculator? Each time I run bc I want scale=2 to be the default, I want to limit all calculations to 2 decimal places. I made a file in my home dir called .bc and inside it I put scale=2…
Bulrush
  • 802
12
votes
1 answer

Installing BC and any maths extension

I need to install bc (I think this is a language so I am guessing I need to install a parser but I seriously have no clue - would I need to install anything else?) onto Ubuntu 12.04 and any additional maths extension that exists for it. The only…
11
votes
9 answers

Command line calculator that keeps fractional values

I just found a good command line calculator program called bc and was satisfied with it until I discovered it rounds off fractional values, thus causing loss in precision. According to its man page: All numbers are represented internally in…
sergionni
  • 523
6
votes
3 answers

Why doesn't `bc` utility exit with `^C`

I was using the bc utility recently and Ctrl+C cannot be used to exit the program. Ctrl+C's interrupt signal is returned with the message use quit to exit. I can use EOF (Ctrl+D) or quit to exit. I read thru the difference between ^Z and ^C.…
5
votes
2 answers

bc: decimal separator comma vs. point

I like using bc in interactive mode as a calculator. However, it only accepts the period (.) as decimal separator but I am using the a German keyboard layout, so I only have the comma (,) available on the numpad. This is quite inconvenient when…
Mq_
  • 153
4
votes
3 answers

Converting Celsius Processor Temperature to Fahrenheit

I'm editing a Conky theme. I would like it to output the processor temperatures in degrees Fahrenheit instead of Celsius. In the ~/.conkyrc file, the command sensors | grep 'Core 0' | cut -c18-19 is used to find the temperature in Celsius for the…
2
votes
1 answer

Can someone confirm this observed bug for `bc`? `scale=0` has no effect!

When I run: fval=1.40 ; echo "scale=0 ; 1000 * ${fval}" | bc I get the result: 1400.00 With my specified value for scale, I expect no decimal digits. Does anyone else have this issue? Version: bc 1.07.1 Ubuntu 22.04.4 LTS (Upgrade of Ubuntu MATE…
1
vote
1 answer

Ubuntu bc weird behavior

There is a weird behavior in Bash script. Trying to run following lines in two different systems. Script: cpuIdle=$(mpstat 5 1 | grep Average | awk '{ print $12 }') cpuUsage=$(bc <<< "100.0-$cpuIdle") And here are the details of…
1
vote
1 answer

getting error with bc - (standard_in) 1: syntax error

I am doing: echo "scale=2; sqrt( (-9.522 - -9.522)**2 + (-17.145 - -17.145)**2 + (-2.689 - -2.689)**2 )" | bc and I am getting: (standard_in) 1: syntax error (standard_in) 1: syntax error (standard_in) 1: syntax error I do not understand what is…
0
votes
1 answer

How can some binary expressed in ASCII 0s and 1s be converted to a binary file?

I've got a PDF expressed as an ASCII file of 0s and 1s, produced in the following way: filepath="Manna.pdf" data="$((echo obase=2; hexdump -ve'/1 "%u\n"' "${filepath}") | bc | xargs printf %08i)" inputText="$(echo "${inputText}" | sed…
-1
votes
1 answer

bc in script doesn't show decimals when dividing

I made this simple script where you're asked the dividend and the divisor, then it displays the quotient: #!/bin/bash read -p "Dividend? " dividend read -p "Divisor? " divisor if [ $divisor = "0" ] then echo "∞" else …
user661429
-2
votes
1 answer

How to use BC with negative exponentials?

I want to use: echo 'scale=6; sqrt(1/4670*1e-06)' | bc But I get: (standard_in) 1: syntax error (standard_in) 1: syntax error