How to Use GNU bc (Basic Calculator) in Linux

bc (Basic Calculator) is a command line utility that offers everything you expect from a simple scientific or financial calculator. It is a language that supports arbitrary precision numbers with interactive execution of statements and it has syntax similar to that of C programming language.

It can be used typically as either a mathematical scripting language or as an interactive mathematical shell as explained in this article.

If you don’t have bc on your system, you can install it using the package manager for your distribution as shown:

$ sudo apt install bc	#Debian/Ubuntu
$ sudo yum install bc	#RHEL/CentOS
$ sudo dnf install bc	#Fedora 22+

To open bc in interactive mode, type the command bc on command prompt and simply start calculating your expressions.

$ bc
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'. 

10 + 5
15

1000 / 5
200

(2 + 4) * 2
12

You should note that while bc can work with arbitrary precision, it actually defaults to zero digits after the decimal point, for example the expression 3/5 results to 0 as shown in the following output.

$ bc
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'. 

3 / 5
0

You can use the -l flag to sets the default scale (digits after the decimal point) to 20 and defines the standard math library as well. Now run the previous expression once more.

$ bc -l
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'. 

3 / 5
.60000000000000000000

5 / 7
.71428571428571428571

Alternatively, you can specify the scale after opening bc as shown.

$ bc
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'. 

scale=0; 8%5
3

scale=1; 8%5
0

scale=20; 8%5
0

scale=20; 8%11
.00000000000000000008

You can also use the following command for common shells for instance in bash, ksh, csh, to pass arguments to bc as shown.

$ bc -l <<< "2*6/5"

2.40000000000000000000

Let’s look at how to use bc non-interactively, this is also useful for shell scripting purposes.

$ echo '4/2' | bc
$ echo 'scale=3; 5/4' | bc
$ ans=$(echo "scale=3; 4 * 5/2;" | bc)
$ echo $ans

To process exactly the POSIX bc language, use the -s flag and to enable warnings for extensions to POSIX bc, use the -w option as shown.

$ bc -s
$ bc -w

For more information, view the bc man page.

$ man bc

That’s all for now! bc (Basic Calculator) is a command line utility that offers everything you expect from a simple scientific or financial calculator. If you have any questions, reach us via the comments section below.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

5 Responses

  1. Kevin Reynolds says:

    Thank you for the reminder. Yes, I had forgotten about bc.

Got something to say? Join the discussion.

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.