Command Line Calculator bc – 11 Extremely Useful Examples

By | 23/05/2013

Q7. I tried 5^5 and I get expected result ie 3125. But, when I do 5^5^5, I get a monster of an output rather than the expected 298023223876953125. Is this some limitation of bc?

Ans. No. This isn’t a limitation. It’s just that you have to use the good old parenthesis to convey what you are trying to do exactly. Here is how you do it :

$ echo "(5^5)^5" |bc
298023223876953125

So you see that after parenthesis were used, the output produced was as expected.

Q8. When I try something like

"ibase=16;obase=10;FFFF" | bc

the output I get is FFFF but when I try

"ibase=16;obase=A;FFFF" | bc

the output is correct, ie, 65535. Why is it so?

Ans. Well, this is because of the fact :

For multi-digit numbers, bc changes all input digits greater or equal to ibase to the value of ibase-1.

So when bc encounters ibase=16, the next number ’10’ (which is the value of obase) is treated as 9base16 and result is calculated according to that. Now, when you specify ‘A’ instead of 10 then as ‘A’ is not a digit so it is not altered.

Besides using ‘A’ as a solution to this, if you try swapping the places of ibase and obase then also the output will be correct.

Here is the example :

$ echo "obase=10;ibase=16;FFFF" | bc
65535

So we see that as obase was read prior to ibase, value of obase was not altered.

Q9. Instead of directly using bc, I want to create a shell script that uses bc to simulate similar behaviour. Is it possible?

Ans. Yes, it is possible. In fact it’s easy. Here is a sample script :

$ cat calc.sh 
#!/bin/bash

bc << END
scale=2
$@
END

Here is the output of the above shown script :

$ ./calc.sh 5*4
20

$ ./calc.sh 5+4
9

$ ./calc.sh 5-4
1

===================================================

RECOMMENDED BY READERS
Linux Is Everywhere – 12 Awesome Devices Powered By Linux
20 interesting and extremely helpful Linux command line tricks

====================================================

Q10. I am trying to calculate

$ echo "scale=10; 1.25*9" | bc

and getting 11.25 but what I really want is 11.2500000000. The variable scale with value 10 is of no help here. What to do?

Ans. Don’t worry, try this :

$ echo "scale=10; 1.25*9/1" | bc

This will give you the expected output ie 11.2500000000.

Q11. Any other striking feature of bc?

Ans. Well, yes. You can even write programs for bc. Can’t believe this, see this example :

$ cat calc_prog 
print "Enter a number A\n"; a = read()

print "Enter another number B\n"; b = read()

if (b == a) {

print "B = A\n";

}

if (a < b) {

print "B > A\n";

}

if (a > b) {

print "B < A\n";

}

quit

Now, run bc in the following way :

$ bc -l calc_prog

Where calc_prog is the name of the file in which above program is saved.

Here is the output of above bc command :

bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
Enter a number A
34
Enter another number B
23
B < A

So we see that bc was able to execute the program and the logic written in it.

NOTE – To get away with the introduction message of bc (highlighted in blue above), use -q flag.

4 thoughts on “Command Line Calculator bc – 11 Extremely Useful Examples

  1. Morpheus

    Now many people would be aware of Douglas Adam’s and his Hitchhikers Guide to the Galaxy, and the answer to the ultimate question being 42 or 6 * 9. How can this be so? Well
    if you do the following in bc:

    obase=13
    6 * 9
    42

    Now to totally confuse students, you can write this on the board and ask them if it is true or false:

    1 + 1 = 10

    Now using bc

    obase = 2

    Press enter enough times to clear the screen then enter

    1 + 1
    10

    Bingo – it is only the smart students who will be wise to what you did!

    Reply
    1. Filipe

      Hi Dude,

      The tricks you wrote are awesome. I’ve been using bc in a while and this is the firs post that I’ve seen something so complete and organized. Of course, I already evernoted your post!

      I know my comment cannot mean anything. However, I know how grateful it can be once you did something and someone really enjoyed that!

      Thank you very much!

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *