# 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
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 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
```

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

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

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

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.

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

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
```
```\$ echo "obase=16; ibase=10; 1000;" | bc
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.