# Questions tagged [bc]

Arbitrary-precision arithmetic language.

109 questions
Filter by
Sorted by
Tagged with
20answers
153k views

### Is there a way to get the min, max, median, and average of a list of numbers in a single command?

I have a list of numbers in a file, one per line. How can I get the minimum, maximum, median and average values? I want to use the results in a bash script. Although my immediate situation is for ...
1answer
7k views

### Why is 'bc' required to build the Linux kernel?

The Linux kernel minimal building requirements specifies that the calculator bc is required to build kernel v4.10, the minimal version of the tool being 1.06.95. What use is made of bc in this ...
5answers
51k views

### Float operation with bc?

It looks like bc doesn't support float operations, when I do echo 1/8 | bc it get me a zero. I checked bc (1), but it doesn't even mention float, so I wonder if it's supported?
4answers
27k views

### How is bc different from dc?

What are the differences between dc and bc calculators? When should I use dc and when bc?
8answers
69k views

### Doing simple math on the command line using bash functions: \$1 divided by \$2 (using bc perhaps)

Sometimes I need to divide one number by another. It would be great if I could just define a bash function for this. So far, I am forced to use expressions like echo 'scale=25;65320/670' | bc but it ...
4answers
3k views

### python vs bc in evaluating 6^6^6

I am evaluating the expression 6^6^6 using python and bc separately. The content of the python file is print 6**6**6. When I execute time python test.py, I get the output as real 0m0.067s ...
1answer
2k views

### bc: Why does `ibase=16; obase=10; FF` returns FF and not 255? [duplicate]

I've been using bc to convert numbers between binary to hex, octal to decimal and others. In the following example, I was trying to convert base 16 (hex) number to binary, octal and decimal. I don't ...
1answer
27k views

### Does bc support hex calculations?

I'm trying to do a hex calculation directly with bc, I already specified the scale. echo 'scale=16;c06b1000-c06a5e78' | bc But I still get a zero. What could be wrong?
5answers
29k views

### Understand “ibase” and “obase” in case of conversions with bc?

I often use bc utility for converting hex to decimal and vice versa. However, it is always bit trial and error how ibase and obase should be configured. For example here I want to convert hex value C0 ...
3answers
39k views

### How to format floating point number with exactly 2 significant digits in bash?

I want to print the floating point number with exactly two significant digits in bash (maybe using a common tool like awk, bc, dc, perl etc.). Examples: 76543 should be printed as 76000 0.0076543 ...
4answers
32k views

### Append to a pipe and pass on?

I have a simple bash function dividing two numbers: echo "750/12.5" | bc I'd like to take the output from bc and append /24 and pipe said result to another instance of bc. Something like: echo "...
4answers
77k views

### Calculate variable, and output it to another variable

The only calculator I know is bc. I want to add 1 to a variable, and output to another variable. I got the nextnum variable from counting string in a file: nextnum=`grep -o stringtocount file.tpl....
2answers
491 views

### Is it possible to save `bc` command line history?

bash has a handy file .bash_history in which it saves the history of commands and on the next execution of bash the history is populated with saved commands. Is it possible to save bc command history ...
3answers
15k views

### BC—automatic full precision multiplication

High, I need to test my arbitrary precision calculator, and bc seems like a nice yardstick to compare to, however, bc does truncate the result of each multiplication to what seems to be the maximum ...
2answers
7k views

### How to show zero before decimal point in bc?

echo "scale=3;1/8" | bc shows .125 on the screen. How to show 0.125 if the output result is less than one?
10answers
2k views

### How can I evaluate a math equation, one per line in a file?

I have a file with the following: 37 * 60 + 55.52 34 * 60 + 51.75 36 * 60 + 2.88 36 * 60 + 14.94 36 * 60 + 18.82 36 * 60 + 8.37 37 * 60 + 48.71 36 * 60 + 34.17 37 * 60 + 42.52 37 * 60 + 51.55 35 * 60 ...
2answers
11k views

### Cannot sum numbers received from stdin using bc

I'm trying to calculate the average entropy of files contained in a folder using: { echo '('; find . -type f -exec entropy {} \; | \ grep -Eo '[0-9.]+\$' | \ sed -r 's/\$/+/g'; echo '0)/'; find ....
4answers
4k views

### how to make bc to show me 10 and not 10.00

#!/bin/bash q=\$(bc <<< "scale=2;\$p*100") head -n\$q numbers.txt > secondcoordinate.txt That's just part of the script, but I think it's enough to clarify my intentions. p is a variable ...
1answer
637 views

### bc: does POSIX prohibit standalone use of relational operators?

I want to compare two numbers with bc. Per this highly rated answer on StackOverflow, I can do it in a manner as such: printf '%s\n' '1.2 > 0.4' | bc bc sends 1 to STDOUT, indicating that the ...
1answer
3k views

### How to define a `bc` function for later use?

I've always found bc kind of mysterious and intriguing. It was one of the original Unix programs. And it's a programming language unto itself. So I gladly take any chance I can find to use it. Since ...
5answers
2k views

### Store 2^500 in a variable in bash

I want to store the value of 2^500 in the variable DELTA. I'm doing export DELTA=\$(echo "scale=2; 2^500" | bc) but this does not set DELTA to ...
3answers
25k views

### Bash limiting precision of floating point variables

In Ubuntu 14.04.1 LTS 64-bit bash I am declearing floating point variables by multiplying floating point bash variables in bc with scale set to 3; however, I cannot get the number of digits after the ...
6answers
2k views

### printf, awk … How to format a number with space to the thousands

Is there a simple way to separate a very large number per thousands with printf, awk, sed ? So 10000000000000 become 10 000 000 000 000 Thanks
4answers
12k views

### Anyway to pass a variable to bc, having a command to be executed?

I need to pass certain variable to bc to get the output in floating point, var1=\$((<some operation>)) var2=\$((<some operation>)) #Needs var1 var3=\$((<some operation>)) #...
2answers
1k views

### how to add command line calculator to bash that uses comma as decimal mark?

How can I add a command line calculator to my bash? I have found some, but all of them use the full stop as decimal mark, but I want to have it to use the comma as decimal mark as most of the world ...
1answer
1k views

### BC not handling scale = 0 correctly

I defined the cbrt function to return a cube root. I need to get an integer back (even if it is close to the cube root, it will be acceptable in my case). However, when I put the scale as 0 to get an ...
4answers
22k views

### Perform floating point arithmetic in shell script variable definitions [duplicate]

I understand bash and some other interpreters only perform arithmetic for integers. In the following for loop, how can I accomplish this? I've read that bc can be used but am not sure how to use bc ...
4answers
863 views

### How to get bc to print trailing zeros?

I read topics about how to get bc to print the first zero, but that is not exactly what I want. I want more... I want a function that returns floating point numbers with eight decimal digits. I am ...
5answers
757 views

### Automatic transformation of newlines in shell variable assignment

This came out of one of my comments to this question regarding the use of bc in shell scripting. bc puts line breaks in large numbers, e.g.: > num=\$(echo 6^6^3 | bc) > echo \$num ...
4answers
4k views

### Difficulty converting from binary to hexadecimal using bc

I read how to calculate using the command line calculator and a HERE-document, but nevertheless I do not get what I expected and can not find my error, what I did in the shell was: bc << HERE &...
2answers
315 views

### How to avoid wrapping in bc output

I have the following example in GNU v. 1.06 (I cannot identify a limit pertaining to line length): v=\$(bc -l <<<"scale=100;4*a(1)"); echo \$v which returns: 3....
2answers
1k views

### Float operations with bc not precise?

When I'm performing float operations in shell using bc, the result is not the same if I use a regular calculator. Am I doing something wrong? For example, I need to find a volume of a sphere. User ...
4answers
38k views

### How to round or convert a float value to int with bc? getting: “(standard_in) 1: syntax error”

I will get value like 2743410360.320 and I want value like 2743410360 to a variable. I tried INTValueOfGB=\$ echo "(\$gb+0.5)/1" | bc But I am getting (standard_in) 1: syntax error
4answers
6k views

### Evaluation an expression and rounding up to three decimals

I have an expression "5+50*3/20 + (19*2)/7" I need to round it up to 3 decimal places. The answer to this is 17.92857142857143. When I use the script below it is giving me 17.928. The answer should be ...
2answers
7k views

### How to sum a bash array of numbers (some in scientific notation)?

Since the following command using bc does not work for numbers in scientific notation, I was wondering about an alternative, e.g. using awk? sum=\$( IFS="+"; bc <<< "\${arrValues[*]}" )
7answers
4k views

### bash - sum numbers in a variable

I have a variable set with numbers separated with space, of which first number can also be led by space, example: VAR=" 2 1 34 3 2 " I need to sum all these numbers. Easiest way would be ...
4answers
12k views

### Shell Scripting: calculate power of a number with a real number as an exponent

I am using shell scripting and I am using the following expression: A=`echo "(( ((\$a / \$b) ^ 0.3) -1 ))" |bc -l` I want to have a real number as an exponent. I noticed that If I place 0.3, it rounds ...
2answers
1k views

### Why does bc exit 0 when dividing by 0?

I was trying to think of a quick and illustrative way to generate a non-successful exit status and thought dividing by zero with the bc would be a good idea. I was suprised to discover that although ...
7answers
1k views

### bc output binary as nibbles separated by whitespace

I'm debugging code which contains quite a few bit shift operations, and I'm using bc a lot to look at what's happening on the bit level. Here's what I use: \$ echo 'obase=2;598980975283696640' | bc ...
4answers
3k views

### Round/truncate digit in string in zsh (or with external tool)

I'm trying to do an interface to bc so it can be used intuitively and without the annoyance of getting "stuck" in it. I haven't got around to test it that much, because I got stuck on another detail, ...
2answers
5k views

### How do I get bc to start decimal fractions with a leading zero

How do I get bc to start decimal fractions with a leading zero? \$ bc <<< 'scale=4; 1/3' .3333 I want 0.3333.
3answers
21k views

### How to calculate values in a shell script?

I run this command in the terminal: grep "bla bla blah" blah* | echo "Blah: \$(wc -l) / \$(ls | wc -l) * 100" And I get this output: Blah: 44 / 89 * 100 What I expect to see: 49.4 Is there a way ...
2answers
1k views

### BC - output normal form?

I have a big number: 2923174917395723957 that would be: 2,923*10^18 are there any parameters in bc that will give this OUTPUT? e.g.: \$ echo '2923174917395723956 + 1' | bc 2,923*10^18 \$ Or ...
2answers
319 views

### How can I do basic calculations in a shell script?

I want to perform some mathematical operations in the shelll. For example: 5+50*3/20 + (19*2)/7 I tried: #!/bin/bash read equ echo "scale=3; \$equ" | bc -l Expected output: 17.929 My output: ...
3answers
148 views

### A bc problem about long expression

I want to calculate an expression in shell. I use the following code: pi=\$(echo "scale=10; 4*a(1)" | bc -l) i=3 d=`expr (1+c(\$pi*(\$i/10)+\$pi))/2 | bc -l` But it says bad pattern: (1+c(3.1415926532*(...
3answers
84 views

### I keep receiving command not found when I try to run my code in bash

I am trying to write a code that will calculate the radius and area of a circle using pi and the circumference. When I try to run my code, I receive an error message that says main.sh: line 5: bc: ...
1answer
306 views

### Start bc with pi defined

I would like to create an alias for bc that runs bc -l and specifies that pi=4*a(1). This way, I can start each session with pi already defined. What alias will do this?
1answer
1k views

### Split input file into X parts of size total lines / X

Currently, my bash script splits by number of lines. However, I'd like to split a file into X pieces, each of those having total lines equal to the file length divided by X. The script is run as ...
3answers
7k views

### How can I call bc from within awk in this case?

OK, so I love my awk, but yeah, it has precision issues, and unfortunately there's no easy way for me to install the multi-precision extensions known as gawkextlib. What I am doing is, I am working ...
3answers
3k views

### BC - no “\\n” at the end + start with zeros?

This is a bc output, e.g.: Input: echo "scale=10; BLA-BLA-HERE-NOT-IMPORTANT" | bc Output: .3708446283953709207058828124021300754352578903651372655882743141882\ ...