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
6
votes
1 answer

round up the bc scale instead of round down with scale or printf

I am using bc and scale for evaluating a expression however I want it to round up instead of round down. What is the simplest way to do this? $ read exp 5+50*3/20 + (19*2)/7 $ echo "scale=3; $exp" | bc -l 17.928 However my desired answer is…
Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
6
votes
2 answers

How to disable line breaks in bc?

Is there a way to disable bc's feature of splitting long numbers into several lines, something like scale variable for controlling decimal places? Sure, I can use sed, but is there a bc way of doing that? Man page at…
Vytenis Bivainis
  • 2,308
  • 21
  • 28
5
votes
4 answers

Difference between printf %.3f and bc rounding behavior

Ok so this is a hackerrank problem (https://www.hackerrank.com/challenges/bash-tutorials---arithmetic-operations). Basically, input is an arithmetic expression and I'm supposed to print out formatted answer (3 decimal places). I tried this at first …
tlaminator
  • 946
  • 2
  • 9
  • 23
5
votes
4 answers

modify value's in CSV with bash-script

I've the following CSV file's: 2012-07-12 15:30:09; 353.2 2012-07-12 15:45:08; 347.4 2012-07-12 16:00:08; 197.6 2012-07-12 16:15:08; 308.2 2012-07-12 16:30:09; 352.6 What I want to do is modify the value in the 2nd column... What I already can do…
Mirco Schmidt
  • 79
  • 1
  • 2
  • 9
4
votes
1 answer

Power of a power in bash with bc

I want to calculate this: 0x0404cb * 2**(8*(0x1b - 3)) which in decimal is: 263371*2^^(8*(27-3)) using | bc. I tried with echo 263371*2^^(8*(27-3)) | bc expr 263371*2^^(8*(27-3)) | bc zsh: no matches found: 263371*2^^(8*(27-3)) or try to resolve…
monkeyUser
  • 4,301
  • 7
  • 46
  • 95
4
votes
1 answer

using bc in bash script

I am trying outputing result in floating point using bc in bash.But I am getting the following output for the following code.How can i get the multiplication result from here and also why i am getting command not found. #!/bin/bash v1=3.41 …
user3708629
  • 153
  • 1
  • 1
  • 7
4
votes
3 answers

Is it possible to use BASH/bc/printf to round a float to its first significant figure?

I've found tons and tons of information about rounding a float to a specific number of decimal places by using 'scale' with BC and '%.xf' with printf, but if I'm working with numbers that don't always have the same format like I've shown below, is…
Thorium
  • 191
  • 1
  • 14
4
votes
1 answer

List all variable defined in bc command

Is it possible to list all variables (print symbol table) in bc command? For example $ bc -q x=3 y=4 z=x+y /* a command for listing all variables defined */ /* and it will show: x 3 y 4 z 7 or maybe in other format */ If saying "all defined…
tsh
  • 4,263
  • 5
  • 28
  • 47
4
votes
3 answers

How to compare two floating-point values in shell script

I had to do a division in shell script and the best way was: result1=`echo "scale=3; ($var1 / $total) * 100"| bc -l` result2=`echo "scale=3; ($var2 / $total) * 100"| bc -l` but I want to compare the values of $result1 and $result2 Using if test…
Reem
  • 1,439
  • 2
  • 18
  • 21
4
votes
4 answers

Why bc and args doesn't work together in one line?

I need help using xargs(1) and bc(1) in the same line. I can do it multiple lines, but I really want to find a solution in one line. Here is the problem: The following line will print the size of a file.txt ls -l file.txt | cut -d" " -f5 And, the…
winter
  • 2,687
  • 4
  • 18
  • 27
4
votes
2 answers

using negative exponents with bc in Bash

I'm having a difficulty with using negative exponents in the program bc in Bash. If I execute echo "2*1.86929*10^05" | bc, I get a result of 373858.00000 while if I execute echo "2*1.86929*10^-05" | bc, I get a result only of 0. How can I get better…
d3pd
  • 7,935
  • 24
  • 76
  • 127
4
votes
4 answers

bash command to sum and print each element of a column and print a new column

Which command should I use to sum the values of two specific columns? For example, I have the file: 1 4 5 1 2 3 5 2 7 8 6 3 And I want to sum the second and last columns, to have the following result 1 4 5 1 5 2 3 5 2 5 7 8 6 3 11 shoud I use awk…
ziulfer
  • 1,339
  • 5
  • 18
  • 30
3
votes
1 answer

(standard_in) 1: syntax error when bc within if

Getting the error: (standard_in) 1: syntax error When running the below script: #!/bin/bash while read line do time=$(echo $line|awk '{print $10}'|awk -F"=" '{print $2}') if (( $(echo "$time > 100" | bc -l) )) then echo $line fi done <…
user973430
  • 43
  • 5
3
votes
2 answers

Why am I getting a syntax error in this bash command on parentheses printf and calculator function?

Why am I getting an error with the ( in this line of script? printf "%.3f\n" "$(bc -l <<< ($sum / $total))" Error: solution.sh: command substitution: line 11: syntax error near unexpected token `(' solution.sh: command substitution: line 11: `bc -l…
3
votes
1 answer

How can I compare floats in bash

Working on a script and I am currently stuck. (Still pretty new at this) First off I have my data file, the file I am searching inside. First field is name, second is money payed, and third is money owed. customerData.txt name1,500.00,1000 …
Tursko
  • 132
  • 1
  • 9
1 2
3
18 19