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:
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:
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:
While using bc command, you can perform different mathematical operations like:
- Add Numbers
- Assign Value to a Variable
- Increment a Variable
- Decrement a Variable
- Compare Two Variables
- Check the Statement is True or False Using the Boolean
- Convert Hexadecimal to Decimal
- Check Condition Using Conditional Statement
- Perform Iteration Using Iterative Statements
- Read through a File
1: Add Numbers
To add numbers from bc command, you can run the following echo command:
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:
3: Increment a Variable
You can also perform increment operation on a variable by using the following bc command:
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:
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:
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:
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:
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:
Here, you can refer an example given below:
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.
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:
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.