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.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

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.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

8 Comments

Leave a Reply
  1. I used Linux for many years. Now I have an HP desktop. I’m very dissatisfied with the way the “Giant” is making changes without my consent. I was trying to change back to LINUX but I’m not sure that all my “simple” contents are getting lost.

    If I can change it back to LINUX without losing it – I will change it immediately. Microsoft changes my products during the night without consent. I would like to change if I do not have losses. Please let me know. Thanks

    Reply
  2. echo "n = 5, x = 3, i = 0, y = 1; a: if (i < n) { i++, y *= x; goto a; } else y;" | cbc

    (It returns 243.)

    C-BC is now on GitHub: https://github.com/RockBrentwood/CBC

    C-BC is significantly larger than GNU BC. I'm fixing to fork and graft GNU BC, itself, into C-BC which is also about to under another upward revision (the switch statement was added in, in the last revision). Among other things, the I/O primitives might be remade into primitives for multi-threaded concurrency in a manner similar to Go.

    Reply
  3. I am using BestCalculator, Shipwreck software, which utilizes bcBasic.

    I’ve written two short programs which work perfectly but the third very similar one using the same variables and the same equation keeps coming up with the left side variable being NaN or, after I set one of the right-hand constants at a value instead of zero, ‘infinity’.

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.