The **bc** or basic calculator is a command-line utility and language that supports arbitrary numbers with interactive execution of statements. It has a syntax that is similar to the c programming language. By using bc you can perform basic mathematical calculations.

In this article, we will discuss the usage of the bc command in the Linux terminal with some examples.

Contents

- 1 How to install bc on Linux
- 2 How to use bc command on Linux terminal
- 3 Using bc in interactive mode
- 4 Using bc in non-interactive mode
- 5 Types of operators supported by bc command
- 6 Example of using the assignment and comparison operators in bc
- 7 Using logical operator in bc
- 8 Example of using maths function in bc
- 9 Conclusion

## How to install bc on Linux

On most of the distribution, it comes preinstalled but if it is not in your system then you can install it by using one of the given commands-

To install in Ubuntu/Linux Mint/Debian use –

sudo apt install bc

If you are using CentOS/ RHEL then use –

sudo yum install bc

On Fedora use –

sudo dnf install bc

## How to use bc command on Linux terminal

The syntax of using the bc command in a Linux system is given below –

bc [options] [ file ... ]

Where you can find the detailed list of options on the man page of the bc command.

## Using bc in interactive mode

When you use the bc command without any argument it will starts in interactive mode. Here you can perform any mathematical operations.

For example –

bc

Write an expression and then press the enter the value will be calculated and displayed to your terminal. You can exit from the bc interactive mode by using the **quit** command.

By default, it gives zero digits after the decimal point for example 1/2 will produce zero you can see this in the given image –

Using option -l with bc command defines the standard math library. It also sets the default scale i.e. digits after the decimal to 20 digits. Now see the example below –

bc -l

Again if we perform division i.e. 1/2 it will print 20 digits after the decimal point.

## Using bc in non-interactive mode

In non-interactive mode, bc takes input from a file or piped with other commands and reads the output of these commands as input, and then processes it. Finally, it displays the result in the terminal.

Some examples of using bc in non-interactive mode are given below-

echo '8+5' | bc echo '5/2' | bc -l

You can see the output below –

## Types of operators supported by bc command

The bc supports the following types of the operator –

- Arithmetic operators
- Increment/decrement operators
- Assignment operators
- Comparison or Relational operators
- Logical or Boolean operators
- Math functions
- Conditional statements
- Iterative statements

## Example of using the assignment and comparison operators in bc

The following example shows the use of the assignment operator and comparison operators with the bc command. The equal to sign i.e. = is known as an assignment operator. Comparison operators are used to comparing two values. If the condition is false it will print 0 otherwise it will print 1.

Now open the bc in interactive mode and try the example that is given in the image.

## Using logical operator in bc

Logical operators are mostly used in conditional statements. The result of this operation will be either 0 or 1.

For example –

`expr1 && expr2`

– will print the 0 if any one of these conditions is false

`expr1||expr2`

– will print 1 if any one of these conditions is true

`!expr1`

– Result is 1 if it is zero

You can see the example in the given image –

## Example of using maths function in bc

The bc provides some inbuilt mathematical functions. For example to evaluate the square root of a number you can use the sqrt() function in bc, similarly to find the number of digits in a number you could use the length() function.

You can see the usage in the given image –

You can find a list of these functions on the man page of the bc command.

## Conclusion

This is how you can use the bc command in your Linux terminal. Now in case you have a query then write us in the comments below.