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

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

- 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

- We can do simple addition by typing a
`+`

sign between two numbers.$ 1+1

- 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

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`

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

- 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

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

- 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

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

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