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
3
votes
2 answers

Bash bc returns (standard_in) 1: parse error only when part of for loop

I'm iterating through an array of integers ${startTimes} (marker locations in an audio file, in samples) and using bc to convert those integers to milliseconds. I'm passing the results into a new array ${msValuesArray}. If I run each array element…
Urphänomen
  • 81
  • 1
  • 8
3
votes
6 answers

Most elegant unix shell one-liner to sum list of numbers of arbitrary precision?

As regards integer adding one-liners, several proposed shell scripting solutions exist; however, on closer look at each of the solutions chosen, there are inherent limitations: awk ones would choke at arbitrary precision and integer size (it…
fgeorgatos
  • 171
  • 1
  • 10
3
votes
2 answers

bash and bc math calculation with decimal point

I am trying to implement the following calculation within a Bash script using "bc" but the "scale" option is producing an incorrect result with 2 additional zeros at the end, which means that I am having to trim it manually…
Arturski
  • 1,142
  • 3
  • 14
  • 26
3
votes
3 answers

In bash how do I divide two variables and output the answer rounded upto 5 decimal digits?

I took two variables as input and after dividing them, I want to get the output rounded up to 5 decimal digits. I have tried this way -> sum=12 n=7 output=$("scale=5;sum/n"|bc) echo $output My code isn't showing any output. What can I…
jbsu32
  • 1,036
  • 1
  • 11
  • 31
3
votes
2 answers

When piping stdout into Perl, how to get it to print a newline character?

I just found out about piping stdout into Perl, and I was amazed that you could even do this: [user@folder] $ echo print 1/3 | perl 0.33333[user@folder] $ From what I understand, you're putting the print command into Perl, and doing a floating…
Tom
  • 919
  • 3
  • 9
  • 22
3
votes
3 answers

How to use bc in bash if statement?

I am trying to use bc to compare a value with decimals in my if statement. I am not sure if I am using bc correctly in my if statement because it doesn't seem to work. It always enters the 4th if statement "IF4". It doesn't seem like the other…
hotlinks0
  • 101
  • 4
  • 12
3
votes
1 answer

unix bc command and order of operation

I'm encountering a strange issue with the basic unix "bc" command on a mac and I want to just make sure I'm understanding it correctly. From the man pages, it says it respects order of operations, which I remember from my schooldays as "My Dear…
infomofo
  • 1,675
  • 2
  • 12
  • 20
3
votes
3 answers

bc not executing via script

Hi I have a script which I am using to calculate the standard deviation from a set of values returned from a server. I can easily retrieve the commands and build a valid string with the command. bc <<< "scale=10;…
Charabon
  • 737
  • 2
  • 11
  • 23
3
votes
1 answer

Convert signed 10 bit binary numbers to decimal?

I have 10 bit signed binary numbers. I know two shell / bash ways to convert them to decimals yet signedness is not recognized. 1111101010 should be converted to -22 and not 1002. echo "ibase=2;obase=A;1111101010"| bc doesn't work. Neither does the…
PiEnthusiast
  • 314
  • 1
  • 4
  • 19
3
votes
1 answer

format output of unix `bc` utility

For large outputs bc utility in unix gives output in this format: $ bc bc 1.06.95 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. 2^1000 …
arunmoezhi
  • 3,082
  • 6
  • 35
  • 54
3
votes
2 answers

Compare two different directories for same file

Is there any tool, which can compare 2 directories( with different sub dirs) for same named files? Eg. DirA has subdir DirA1, DirA1 has one file named file.cpp. DirB has file.cpp. Dira->DirA1->file.cpp and DirB->file.cpp. So, iF i compare these 2…
Bolt
  • 33
  • 1
  • 3
2
votes
1 answer

`printf "2+2" | bc` returns a syntax error, `echo "2+2" | bc` works, how are they handling strings differently?

printf "2+2" | bc gives me a syntax error, but echo "2+2" | bc outputs 4, I thought printf would work fine but it didn't, could someone help me understand why?
phenivol
  • 41
  • 5
2
votes
0 answers

Is there mathematical (or historical) motivation for including a Bessel function in the GNU bc math library?

The math library for the GNU basic calculator bc defines a few standard primitive 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…
Mike Pierce
  • 1,390
  • 1
  • 12
  • 35
2
votes
1 answer

bash bc command converts uppercase letters to 9

I pipe different values to bc. If the value is a number, it works fine. If it's a string with lowercase letters, it returns 0 which makes sense to me, but if it's uppercase letters, bc converts it to 9 as the length of the input characters: echo 1 |…
Ömer An
  • 600
  • 5
  • 16
2
votes
2 answers

How to print float numbers with awk

I have this input file: Adrian 20.6 Bruce 40.2 Adrian 30.4 Janick 24.2 Dave 42.7 Bruce 42.9 Janick 41.2 And with a answer I've got here, I was able to sum the numbers, but now the numbers are float and I don't know how to print them. For now, my…
Lvcrft
  • 57
  • 5