Home > linux > Command line calculator for Linux, calculate big with bc

Command line calculator for Linux, calculate big with bc

February 25th, 2010 Leave a comment Go to comments

If you’re a command line freak, you’ve probably use expr for common math calculations but did you know that the bc command allows you to do similar math calculations and is more powerful when it comes to bigger calculations?

bc is a language that supports arbitrary precision numbers with interactive execution of statements. There are some similarities in the syntax to the C programming language. A standard math library is available by command line option. If requested, the math library is defined before processing any files. bc starts by processing code from all the files listed on the command line in the order listed. After all files have been processed, bc reads from the standard input. All code is executed as it is read. (If a file contains a command to halt the processor, bc will never read from the standard input.)

If the above man page description don’t make any sense yet, then just remember that bc is a command line calculator. The nice thing about bc is that it accepts inputs from files, from standard output and this allows the command line user to pipe data out for quick calculations. The syntax for basic calculation is very similar to Google’s calculator.

Here are some basic examples:

$ cat file.txt
$ bc < file.txt
echo 2+2 | bc

It’s possible to use basic math with variables:

$ A=9
$ B=6
$ C=$ ((A+B))
$ echo $C

Some more basic examples of addition, subtraction, multiplication and division:

$ echo ’50+20′ | bc
$ echo ’50-20′ | bc
$ echo ’50*20′ | bc
$ echo ’50/20′ | bc

There is a variable called scale and using it adds number of digits which follow the decimal point. By default scale is 0. So the above example shows 2 but if scale variable was used, the result could be displayed in x # of digits.

$ echo ‘scale=1;50/20’ | bc

How about square root, and power:

$ echo ‘scale=15;sqrt(8)’ | bc
$ echo ‘4^4’ | bc

Convert from decimal to hexadecimal, from decimal to binary and from binary back to decimal:

$ echo ‘obase=16;255’ | bc
$ echo ‘obase=2;22’ | bc
$ echo ‘ibase=2;obase=A;10110’ | bc

bc in interactive mode:

$ bc

As you can see, it’s not shy about displaying the big value results. Here are some more resources to help you explore bc further:

  1. man bc (Yes man page, it’s got everything you’ll need)
Categories: linux Tags:
  1. adddddsa
    February 26th, 2010 at 08:34 | #1

    Nice overview, thanks.

  2. February 26th, 2010 at 15:41 | #2

    Nice article. I love to learn new (to me) useful Unix commands. My calculator keeps getting lost amongst the paperwork and other stuff on my workstation. This will come in handy.

  3. PacMan
    February 27th, 2010 at 10:21 | #3

    FYI: You might want to use ‘ bc -l ‘ for anything with decimals. I have expericances in the past where bc will just round the number.

  4. April 30th, 2010 at 05:24 | #4

    lol cool stuff bro.

  5. Anil
    April 9th, 2011 at 01:15 | #5

    I have a file named file.txt that contains binary number. Let the binary number be 1000101. In place of writing

    print “Enter binary numbers: ” ; bin = read()

    I want the input should be taken from file.txt. Something like <file.txt

  1. February 26th, 2010 at 10:04 | #1