bc command in Linux with examples

Most users naturally think of a calculator on their computer as a GUI application, or something that is accessed directly from the desktop. But it’s also possible to use the bc command in Linux to run calculations through the command line terminal. bc is an acronym for “basic calculator.”

If you’re familiar with the C programming language, you’ll probably notice that the syntax for the bc is quite similar. The calculator can also handle variables and algebra, or do other useful things like convert numbers to hexadecimal.

In this tutorial, you’ll learn how to use the bc command in Linux through examples. Follow along below to learn about the various options that you can use to perform calculations with this command.

In this tutorial you will learn:

  • How to use the bc command on Linux
bc command in Linux with examples
bc command in Linux with examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software bc
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options



bc command in Linux Basic Examples

  1. There are two main ways in which the bc command works. The first is through entering an interactive mathematical shell. We can enter the shell by simply running the bc command by itself. Once we’re in, we can start executing some basic mathematical operations. And the second way that bc works is as a mathematical scripting language, but that will be covered later.
    $ bc
    

    Using bc by itself to enter the interactive mathematical shell to do basic calculations.
    Using the bc command by itself to enter the interactive mathematical shell to do basic calculations.
  2. We can do simple addition by typing a + sign between two numbers.
    $ 1+1
    

    Using the bc command to calculate 1+1
    Using the bc command to calculate 1+1
  3. We can also do multiplication and division. For division, we’ll simply use a / forward slash between two numbers. And for the multiplication operation, we’ll need to use the * asterisk instead.
    $ 4/2
      2
      2*2
      4
    
    Using the bc command to calculate 2x2 and 4/2
    Using the bc command to calculate 2×2 and 4/2

    As you can see in the screenshot above, pressing enter upon typing out a mathematical operation, bc will calculate the equation directly below.

    This pattern will continue until you exit the interactive mathematical shell. You can achieve this by using the Ctrl + D key combination or typing quit.

  4. But we can also perform mathematical operations in the Linux terminal using bc without entering the interactive shell. For this process, we’ll need to utilize the echo command and | pipes. This will echo any text we type and pass it to the bc command.
    $ echo 1+1 | bc
    

    Using the echo command and piping the output to the bc command to do calculations without entering the interactive shell
    Using the echo command and piping the output to the bc command to do calculations without entering the interactive shell


  5. We can also use bc to compute numerous mathematical operations that are contained in a text file. Simply pass a text file filled with math equations to the bc command and let it do its job.
    $ bc file01
    
    Using the bc command to calculate mathematical equations that are present in a text file
    Using the bc command to calculate mathematical equations that are present in a text file

    Though we didn’t include it in the example syntax, it’s important to note that we can use < to omit the description when running the bc command for a cleaner output.

NOTE
You can always use the man command to read more about the bc command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Advanced Usage

While bc is pretty simple, it can be used to solve more complex math problems with variables as you’ll see below.

bc command in Linux Advanced Examples



  1. As mentioned earlier, bc is also used as a mathematical scripting language. Because of this, we can also utilize the bc command to determine mathematical variables in the Linux command line terminal.
    $ $ echo "x=1; x+=2;x" | bc
    
    Determining variables using the bc command and the echo command
    Determining variables using the bc command and the echo command

    As you can see in the syntax above, we surrounded the variables following the echo command in quotation marks. This is important to do for this specific operation as you are liable to receive an error otherwise.

  2. Continuing down the mathematical scripting language application of the bc command, we can also perform boolean operations where 1 is true and 0 is false. We can achieve this by, once again, utilizing the echo command with pipes.
    $ echo “1<=2” | bc
    
    Using bc with the echo command to perform boolean operations
    Using the bc command with the echo command to perform boolean operations

    As you can see in the screenshot above, we passed to echo the statement that “1 is less than 2”. And because this statement is true, piping it to bc will give us an output of 1.

  3. Converting from a decimal to hexadecimal number is even easier. All we need to do is specify an output base ( obase ) and an input base ( ibase ). Let’s convert the decimal number 1000 to a hexadecimal number:
    $ echo "obase=16; ibase=10; 1000;" | bc 
    3E8
    


Closing Thoughts

In this tutorial, we learned all about the bc command on Linux. Using bc is essential to master for users and administrators who make basic calculations or programming scripts on Linux frequently. The command becomes even more useful when combined with echo or other commands, as bc can read the output from those commands and perform calculations on the fly.