The bc command in Linux – How to Perform Mathematical Operations in Linux Shell?

Bc Command

The bc command, short for basic calculator, is a language that supports arbitrary precision numbers with interactive execution of statements. Its syntax is similar to that of C programming language. It has two major applications. One as a mathematical scripting language and the second as an interactive mathematical shell.

In this tutorial, we will learn how to use the bc command for performing mathematical, logical and boolean operations.

How to use the bc command as an interactive mathematical shell?

To enter the interactive mathematical shell type :

bc

Press Enter.

Bc
bc

Now you can start performing mathematical operations.

Let’s try out some :

5+5

Output :

10

Let’s try another one :

7/3

Output :

2

Here are some other operations :

Mathematical Operations
Mathematical Operations

How to use the bc command along with the echo command?

You can use the bc command to compute mathematical operations without entering into the interactive mathematical shell. To perform mathematical operations and output the result, you can use the echo command along with bc.

The syntax for doing that is :

echo "3+9" | bc

Output :

12

Let’s try another one :

 echo "3*9" | bc

Output :

27

Here’s another one :

echo "3/9" | bc

Output :

0
Echo

How to change the number of digits after the decimal?

You can set the number of digits after the decimal by changing the value of scale parameter. The syntax for doing that is:

 echo 'scale=25;100/23' | bc 

Output :

4.3478260869565217391304347

Let’s try another value for scale.

 echo 'scale=10;100/23' | bc 

Output :

4.3478260869
Scal
Scale

How to provide input from a file?

You can also provide the input from a file. To do that let’s create a file with multiple lines of mathematical operations in it.

We will use the cat command for creating a file.

 cat > bc_calc.txt 

The text for the file is as follows:

5+7
7*7
20/5

To give this file as input to bc command use :

bc < bc_calc.txt

Output :

12
49
4

How to convert from Binary to Decimal?

Bc also lets you convert from one number system to another. To convert from binary to decimal you can use :

'ibase=2;obase=A;11' | bc

Output :

3

Let’s try another one :

'ibase=2;obase=A;111' | bc

Output :

7
Binary To Decimal
Binary To Decimal

How to convert from Decimal to Binary?

To convert from Decimal to Binary you can use :

'ibase=10;obase=2;3' | bc

Output :

11

Let’s try another one :

'ibase=10;obase=2;7' | bc

Output :

111
Decimal To Binar
Decimal To Binary

How to declare variables under bc?

Since bc is also a mathematical scripting language you can declare variables for performing mathematical operations. The syntax for doing that is :

echo "x=15; x+=10;x" |bc

Output :

25

Let’s try another one :

echo "x=15; x*=10;x" |bc

Output :

150
Variables

How to perform boolean operations under bc command?

You can also use the bc command to perform boolean operations.

echo "11<=7" | bc

Output :

0

0 is equivalent to false.

Let’s try another one :

echo "11>=7" | bc

Output :

1

This is equivalent to true.

Boolean Operations
Boolean Operations

Conclusion

This tutorial was about the bc command in Linux. We learned how to use this command for performing mathematical, logical, and boolean operations. To read more on the bc command, refer to the documentation. Alternatively, you can use the man command to read the manual.