Linux Commands

bc Command in Linux With Examples

bc is a command line tool that allows users to perform mathematical operations the same as they do on a calculator. The users can do operations like addition, multiplication, division and more right on the terminal. The tool is preinstalled on the Linux system. However, those users who don’t know how to use the bc command should follow this tutorial for further details.

bc Command in Linux

There are different ways to use bc command in Linux, let’s guide you by presenting several examples.

To use bc command for performing mathematical operations, first, you need to enter the bc command line shell by executing the command given below:

bc

After entering the shell, you can enter the input of your choice and the operator between two input numbers.

Example 1
To add two numbers, use the “+” operator between the two numbers and press Enter to get the result.

Example 2
Let’s multiply two numbers by entering, use asterisk sign between the two numbers:

4*5

If you don’t want to use the bc command line shell, you can use the echo command followed by the mathematical operation piped with the bc command as shown in the below-given syntax:

echo <Operation> | bc

While using bc command, you can perform different mathematical operations like:

1: Add Numbers

To add numbers from bc command, you can run the following echo command:

echo 2+2 | bc

Using the same syntax as above you can perform other arithmetic operations like subtraction, multiplication, and division.

2: Assign Value to a Variable

bc command also supports the operations of assigning a value to a variable and this can be done through the following command:

echo "var=10;var" | bc

3: Increment a Variable

You can also perform increment operation on a variable by using the following bc command:

echo "var=10;++var" | bc

4: Decrement a Variable

Similarly, you can use decrement operator to reduce the value of a variable and this can be done via the following command:

echo "var=10;--var" | bc

5: Compare Two Variables

You can also compare two variables’ values using the bc command and get the result whether the condition is true or not. If the condition is true, you will see the output as 1 otherwise it’s 0:

echo "10>5" | bc

Note: You can use any condition you want.

6: Check Whether the Statement is True or False Using the Boolean

You can also perform logical or Boolean operations using bc command to check whether the given statement is true or false.

For example, the below command checks whether both numbers are non-zero, if the condition is satisfied, it outputs the result 1:

echo "10 && 5" | bc

7: Convert Hexadecimal to Decimal

You can perform various mathematical functions like conversion of number systems using bc command.

For example, the following commands will convert the number 10 to octal from binary and the resultant of this conversion is 2:

echo "ibase=2;obase=8;10" | bc -l

8: Check Condition Using Conditional Statement

You can also execute the commands with conditional statements using the bc command by following the syntax given below:

if(condition) {statements} else {statements}

Here, you can refer an example given below:

echo 'n=8;m=10;if(n>m) print "n is greater" else print "m is greater" ' | bc -l

9: Perform Iteration Using Iterative Statements

If you are interested in executing a loop and printing the values, you can use the following bc command inside the echo statement.

echo "for(i=1; i<=10; i++) {i;}" | bc

10: Read Through a File

The bc command can read the operations written in a file, execute them and show the results on the command line terminal.

For example, you have a file, which includes some arithmetic operations between 2 numbers. When you execute the command given below you would get the output of the operations to perform inside the file:

bc Test

Conclusion

The bc commands are useful for users who want to perform calculations on the Linux system. The bc commands can be executed with echo to perform a specific operation like adding two numbers, subtracting two numbers, incrementing a variable value, and much more. The above-mentioned guidelines provide different bc commands that are useful for Linux beginners in performing different types of calculations on the terminal.

About the author

Rafia Amjad

I have a degree in Electronics and love to write. My research and writing emphasize on the most recent innovations in gaming and technology.