bc: Command line calculator

If you run a graphical desktop environment, you probably point and click your way to a calculator when you need one. The Fedora Workstation, for example, includes the Calculator tool. It features several different operating modes that allow you to do, for example, complex math or financial calculations. But did you know the command line also offers a similar calculator called bc?

The bc utility gives you everything you expect from a scientific, financial, or even simple calculator. What’s more, it can be scripted from the command line if needed. This allows you to use it in shell scripts, in case you need to do more complex math.

Because bc is used by some other system software, like CUPS printing services, it’s probably installed on your Fedora system already. You can check with this command:

dnf list installed bc

If you don’t see it for some reason, you can install the package with this command:

sudo dnf install bc

Doing simple math with bc

One way to use bc is to enter the calculator’s own shell. There you can run many calculations in a row. When you enter, the first thing that appears is a notice about the program:

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

Now you can type in calculations or commands, one per line:

1+1

The calculator helpfully answers:

2

You can perform other commands here. You can use addition (+), subtraction (-), multiplication (*), division (/), parentheses, exponents (^), and so forth. Note that the calculator respects all expected conventions such as order of operations. Try these examples:

(4+7)*2
4+7*2

To exit, send the “end of input” signal with the key combination Ctrl+D.

Another way is to use the echo command to send calculations or commands. Here’s the calculator equivalent of “Hello, world,” using the shell’s pipe function (|) to send output from echo into bc:

echo '1+1' | bc

You can send more than one calculation using the shell pipe, with a semicolon to separate entries. The results are returned on separate lines.

echo '1+1; 2+2' | bc

Scale

The bc calculator uses the concept of scale, or the number of digits after a decimal point, in some calculations. The default scale is 0. Division operations always use the scale setting. So if you don’t set scale, you may get unexpected answers:

echo '3/2' | bc
echo 'scale=3; 3/2' | bc

Multiplication uses a more complex decision for scale:

echo '3*2' | bc
echo '3*2.0' | bc

Meanwhile, addition and subtraction are more as expected:

echo '7-4.15' | bc

Other base number systems

Another useful function is the ability to use number systems other than base-10 (decimal). For instance, you can easily do hexadecimal or binary math. Use the ibase and obase commands to set input and output base systems between base-2 and base-16. Remember that once you use ibase, any number you enter is expected to be in the new declared base.

To do hexadecimal to decimal conversions or math, you can use a command like this. Note the hexadecimal digits above 9 must be in uppercase (A-F):

echo 'ibase=16; A42F' | bc
echo 'ibase=16; 5F72+C39B' | bc

To get results in hexadecimal, set the obase as well:

echo 'obase=16; ibase=16; 5F72+C39B' | bc

Here’s a trick, though. If you’re doing these calculations in the shell, how do you switch back to input in base-10? The answer is to use ibase, but you must set it to the equivalent of decimal number 10 in the current input base. For instance, if ibase was set to hexadecimal, enter:

ibase=A

Once you do this, all input numbers are now decimal again, so you can enter obase=10 to reset the output base system.

Conclusion

This is only the beginning of what bc can do. It also allows you to define functions, variables, and loops for complex calculations and programs. You can save these programs as text files on your system to run whenever you need. You can find numerous resources on the web that offer examples and additional function libraries. Happy calculating!

Fedora Project community

23 Comments

  1. Why publicize a command line calculator in a time when the computer should read the mind of people and execute the operation ( i’m exceeding, I know ).
    There are already, formidable and computational complex online tools that allow to do this, see wolfram.
    Or very complex software like matlab even if not free.
    What I perceive is that linux doesn’t have innovation to offer than a calculator and this is quite sad
    Cit “Is this (is) the end my only friend”

    • Sebastiaan Franken

      Linux has more to offer than a GUI. Some people use it to work on and some of those people (I am one of those) have automated a great number of tasks, some of which include math. I don’t want my scripts to halfway stop and tell me “Hey, can you open the fancy calculator app you have and do some math for me? I’ll wait.”. No, I want my script to be able to do said math and just carry on and only tell me something when it’s done or when there’s an error. I use bc in a few scripts to calculate averages and do conversions from base-16 to base-10.

      That way I can get more work done (in the same amount of time). Linux is all about choice and the freedom to choose the tool that works for you. Like I said, bc is a lifesaver to some of us

  2. Shorter:

    bc <<< 1+1

  3. Great and very useful post. Will apply in my work-flow. Thanks!

  4. new users can try more if you follow https://docs.fedoraproject.org/en-US/index.html
    also the dnf is a great tool like yum the repo is the same …

  5. This is a very good article about bc 🙂

    There is also a -l flag for bc which says to define the math lib, which also gives the answer in the decimal points

    echo ‘ 3 / 5 ‘ | bc -l

  6. Elliot

    An easier way to avoid issues with scale is to invoke bc with the -l (–mathlib) option.

  7. Brett

    I love bc and use bc every day.

    I have bc aliased to “bc -lq”. Sets a reasonable scale and drops the startup banner.

  8. Saman

    Thank you so much. I’ve noticed “scale” doesn’t do the rounding. for example If I want to get the result of 3 / 4 using scale=1, the result is 0.7 instead of 0.8.

  9. ! need time to follow the all docs …

  10. Brett

    One thing I like to do when demonstrating bc is this:

    2^4096

    🙂

    Brett

  11. Great info, I wasn’t aware of bc, learning something every day! Many thanks for the posting. I use the cli frequently and now don’t need to switch back to a gui for quick calculations ….. many thanks!

  12. hyhy123

    Really great! I find this useful in some cases, especially when I am writing a shell script and find there are some simple math calculations. The bc can get things done perfectly!

  13. c4ifford

    why not just use python?

  14. rpndude

    Being a rpn person, I usually use ‘dc’; but ‘bc’ is great to, especially in scripts.

  15. Tad Marko

    I’d recommend dc.

  16. Dale Raby

    I never knew about this. I’ve been using an actual slide rule to do quick estimates for an “out the door price” for customers as it takes a few seconds to bring the GUI calculator up. As I always have a shell up, I can open bc and type 299.99+13+1.05(enter) and get $314.04. Yeah, I know I could invest a dollar and get a pocket calculator to replace the one that broke a couple months ago, but I always seem to “lose” those when they are on the counter and yeah, I know I could multiply by 10% and divide by two using my actual organic brain… but this is way cooler!

    • Dale Raby

      Hmmm… a problem manifests itself. You have to remember to hit the correct keys! 299.99+13*1.05
      313.64

      • Dale Raby

        As I said, the CORRECT keys:
        (299.99+13)*1.05
        328.63
        customers would probably prefer I forget about the brackets…

      • Dale Raby

        Ooops…
        (299.99+13)*1.05
        328.63
        customers would probably prefer I forget about the brackets…

Comments are Closed

The opinions expressed on this website are those of each author, not of the author's employer or of Red Hat. Fedora Magazine aspires to publish all content under a Creative Commons license but may not be able to do so in all cases. You are responsible for ensuring that you have the necessary permission to reuse any work on this site. The Fedora logo is a trademark of Red Hat, Inc. Terms and Conditions