bc Command in Linux
The bc command is used for precision calculation. You are more likely to use it for floating point operations in bash scripts.
The bc command in Linux is used for mathematical calculation. It's more than just a command; it's a language. But you are likely to use it as a command for floating point mathematical operations.
Yes. unlike other programming languages, bash does not have a float variable option.
Apart from the floating point operations, you can also use it in the terminal for arithmetic operations.
In this tutorial, I will cover both ways to use the bc command: in the bash script and in the terminal window.
How to use the bc command in Linux
To use the bc command, you'd have to follow the given command syntax:
bc [options] [file]
[options]is used to change the default behavior of the bc command.
[file]is where you append the file to perform calculations from data within the file.
[file] are optional which means you can use the bc command without any of these.
To use the bc command in the terminal, all you have to do is execute the
bc command and it will open a prompt where you can type arithmetic operations:
As you can see, it can easily calculate operations with decimals.
To exit, you can either type and execute
quit or press
Ctrl + D.
Now, let's have a look at some examples.
1. Deal with decimals
As I mentioned in the beginning, the bc command is used when your output has decimals, but by default, it won't show any.
Now, there are two ways to manage decimals:
- Executing the bc command with the
- Using the
scaleoption (gives flexibility)
Let's start with the first one.
Using the bc command with the -l
When you execute the bc command with the
-l flag, it loads the math library which enables you to use all the advanced calculation functions.
And if you wish to do so, you can be done with the following:
The only downside of using this method is it will show around 20 decimal placeholders every time you perform a division.
Using the scale option
This is another method where you use
scale and specify how many decimal placeholders you want.
For example, if I were to divide
2, then, the answer should be
4.5 so I have to have one decimal placeholder. So I will use
As you can see, how the output differs with and without the
2. Calculate without the bc prompt
Alternatively, you can get a similar output without using the bc prompt.
To do so, you can write any arithmetic operation with the scale option to decide how many decimals there should be and pipe it to the bc command:
echo "scale=no_of_decimals; arithmatic_operation_here" | bc
For example, here, I want to divide 15 by two so I have to have one decimal, then, I will use
scale=1 and pipe it to the bc command:
echo "scale=1; 15/2" | bc
3. Use variables with the bc command
The usage of variables with the bc command is quite straightforward. All you have to do is execute the bc command, assign some variables, and then perform arithmetic operations with variables.
First, execute the
Then assign values to variables. Such as here, I assigned 2 to
After that, you can perform any arithmetic operation you want to. For example, here, I did the addition:
4. Use the math library with the bc command
Using the math library, you can find out the value of trigonometry functions, logarithms, and a lot more.
First, start the bc with the
-l flag to load the math library
So let's say you want to find out the value of the sin 0, then you can use the following:
You can do the same with all the trigonometry functions.
And if you want to find out the log values, use the following syntax:
Here's an example of how you use the math library with the bc command:
5. Use the bc command in the bash script for floating point operations
Using the bc command in the bash script is pretty much the same as I explained in how you use the bc command without the bc prompt part.
In simple terms, you use the variable that stores the
scale option (if any) and output of the arithmetic operation:
variable=$(echo "scale=no; arithmetic_operation" | bc)
For example, here, I created a simple bash script that asks users to enter two numbers for division and then, it uses the bc command with
scale=3 to handle float values:
#!/bin/bash read -p "Enter the first number: " num1 read -p "Enter the second number: " num2 result=$(echo "scale=3; $num1 / $num2" | bc) echo "Output: $result"
If you want to use the above script, then, make sure to make it executable using the chmod command.
When executed, you can expect the following behavior:
6. Use the bc command using the file
This is quite a convenient way of calculating operations as all you have to do is write the actions only one time in the file and can be used endlessly.
First, create a file with the touch command. I would recommend creating a file with the
.bc extension so you can have a better idea about what is inside.
Now, you can use any of your favorite text editors to make changes. Here, I will be using the nano text editor:
Here, I'm going with simple arithmetic operations, but you can make changes as per your needs:
x = 10 y = 3 x / y
Once done, save changes and exit from the nano text editor.
Now, all you have to do is redirect the input from a file to the bc command:
bc -l < calculation.bc
The only reason why I used the
-l flag is I didn't use the
scale option in my file but if you did, you can avoid the
Optionally, you can save the output by directing it to a file:
bc -l < calculation.bc > Result.txt
More on bash
If interested, check out this tutorial on using mathematical calculations in bash scripts.
Something more for you on special variables in bash.
I hope you will find this guide helpful.