Command line calculator, bc
September 4th, 2006 mysurface Posted in bc, Calculation, pipeline | Hits: 124974 | 21 Comments »
How to do calculation if I only have command line? If you have BC you can, and you can do a very complicated calculation. To perform calculation, you can type bc and start to type your question, or passing question through pipeline, such as
echo "56.8 + 77.7" | bc
Let say you want to convert decimal to hexadecimal, you can do this.
echo "obase=16; ibase=10; 56" | bc
ibase is input base, and obase is output base, by specified the base, you can convert number from one base to another.
To perform division is a bit tricky, because the will result usually in floating point. Therefore in order to get the correct answer, you need to specified scale. scale means the precision of floating point, how many digit after the point. By default the scale is 0, that means it is integer.
5.00500 – the scale is 5
5.005 – the scale is 3
So the example :
echo "scale=6; 60/7.02" | bc







September 20th, 2006 at 6:11 pm
A simply way to do calculation with floating point
March 11th, 2007 at 9:09 am
bc -l works, but it sets scale=20 which gives:
1/3
.33333333333333333333
most of the time I just want two decimal places.
there may be an easier way, but what works for me is the following:
in .bashrc in your home directory add BC_ENV_ARGS=~/.bc
export BC_ENV_ARGS
then create .bc also in your home directory
with one line
scale=2
then when bc is run
1/3
.33
March 12th, 2007 at 8:45 am
Cool in that way you can set a default configuration for bc. You can also simply type
scale=2while you are in the interactive mode.April 14th, 2007 at 2:28 am
[...] First you need to calculate what time you wanna wake up? Let say if you wanna wake up after 5 hour and 30 minutes. You need to calculate the time in seconds using bc and parse the result to sleep, and later play a song to wakes you up after the sleep: [...]
April 24th, 2007 at 2:49 am
To use the last value just use the word last in interactive mode.
1+1
>2
last+2
>4
last + 5
>9
last +8
>17
quit
September 23rd, 2007 at 5:01 am
Nice post. I also use “prepend” to deal with pipeline problems that occur. For example:
randy@pseudo ~/prng $ echo “13203903″ | prepend “obase=2;ibase=10;” | bc
110010010111100110111111
Otherwise, you can’t enter the obase/ibase stuff without messing up the pipeline.
But as for bc, I don’t use it much, and prefer my “pc” perl calculator. It does normal math, but I mostly use it for advanced financial calculations.
randy@psion /usr/src $ pc
Entering interactive mode, use “expression” to run in batch mode.
>1000000=50000(F/P,i%,30)
Result: 0.105013847351074 @ 1000003.71937184, aiming for 1000000
10.50%
Interpreted: Find the interest rate ‘i’ for which the ‘F’uture value is 1 million given a ‘P’resent value of $50,000 invested for 30 years.
I think it is up at my website http://nosatalian.homelinux.com
May 28th, 2008 at 6:51 pm
Hi all, great examples here, but I need to compare version no for some programs like bios version 2.3.20 vs 2.3.21, how to do it in bc. thanks
April 8th, 2009 at 5:52 pm
Remember too, that to end a bc session you don’t need to type “quit” (unless you want to). You can simply use CTRL-D. Same for the current shell, by the way. I often open a terminal just for bc. When I’m finished with it I simply tap CTRL-D twice. The first ends bc, the second closes that shell.
April 15th, 2009 at 7:33 am
echo “obase=36;44444″|bc produces three decimal digits. How do you convert them to YAK which is base36 output in Perl, OO, JS etc., iow, a true base36 number (0-9,a-z).
October 21st, 2009 at 5:12 am
Another handy way of using bc.
bc <<< 56.8+77.7
November 21st, 2009 at 6:07 pm
This is great. A built-in calculator that will do calculations in base 6 (or base x) and can easily be set to convert inputs of one base to output of another. It seems that it would be simple to adapt one of the gui calculators based on the bc engine to to the same.
bc
scale=30
obase=6
1/17
>.020412245351433102041224535143310204121
quit
Well, there is a rounding error on the last “digit” (hexit?). The scale (base 10) is expanded (or contracted) in relation to the output base.
It looks like bc uses letters for “digits” up to base 16 then resorts to 2-digit pairs. It seems reasonable to adapt it to use letters for “digits” up to base 36.
December 26th, 2009 at 11:15 am
Hi,
rtfm:
snip–
The legal values of ibase are 2 through 16. Assigning a value outside this range to ibase will result in a value of 2 or 16. Input numbers may contain the characters 0-9 and A-F. (Note: They must be capitals. Lower case letters are variable names.)
–snap
some examples:
10->16:
~$ a=$(echo “obase=16; 15+0″ | bc -l)
~$ echo $a
F
16->10:
~$ b=$(echo “ibase=16; obase=A; F+1″ | bc -l)
~$ echo $b
16
–> note that obase must be given in ibase-style
10->2
~$ c=$(echo “obase=2; 10+0″ | bc -l)
~$ echo $c
1010
2->10
~$ d=$(echo “ibase=2; obase=1010; 10+10″ | bc -l)
~$ echo $d
4
etc.
best regards
January 30th, 2010 at 6:50 pm
You might want to save the result of your calculation to a file. This is easy to do with echo and &>.
echo \\&"scale=1000; sqrt(2)\\&" | bc &> sqrt2.txt
This will print sqrt (2) to 1000 decimal places to the file sqrt2.txt.
April 16th, 2010 at 3:15 pm
how count all lines in all files in current dir and omit empty lines with wc, grep, cut and bc commands
echo `wc -l * | grep total | cut -f2 -d’ ‘` – `grep -in “^$” * | wc -l ` | bc
May 19th, 2011 at 2:03 am
I get pleasure from, lead to I discovered exactly what I was having a look for. You’ve ended my four day lengthy hunt! God Bless you man. Have a nice day. Bye
June 22nd, 2011 at 4:18 pm
Hey there, You have done a wonderful job. I will definitely digg it and i really believe recommend to my friends. I am sure they will bebenefited from this site.
July 9th, 2011 at 9:14 pm
Von der Seite habe ich das noch gar nicht gesehen.
September 14th, 2011 at 12:06 pm
Calculate the number of bits needed to represent the number (9.337 * 10^426). NOTE: this number is significant if you follow GRC’s “Off The Grid” password generator, https://www.grc.com/otg/security.htm, it’s the lower bound of the possible number of 26×26 latin squares.
The calculation to figure out how many bits would be needed to represent the above number, 9.337 * 10^426, is thus:
echo “l(9.337*10^426)/l(2)”|bc -l
1418.36432750510331443454
or roughly 1418 bits.
October 19th, 2011 at 9:33 pm
Thanks for the export BC_ENV_ARGS hint! I had never guessed how to set defaults for bc.
February 17th, 2012 at 8:47 pm
yeah, .bcrc idea helped me get rid of another annoyance as well. scale=2 ftw :>
June 27th, 2012 at 8:15 pm
Mike, maybe this code to work in bashrc without the need to create an extra file:
BC_ENV_ARGS=$(<<< scale=2) && export BC_ENV_ARGS