BC, which stands for Basic Calculator, is a command in Bash that is used to provide the functionality of a scientific calculator within a Bash script. This can be useful for scripting with various arithmentic use cases and scenarios. This article shows you how to use BC in a Bash script.

Examples of using the BC Command in a Bash Script

To learn more about using the BC command in a Bash script in Linux Mint 20, take a look at the examples provided in the following sections:

Example 1: Calculating the Power of a Number

Bash programming only allows us to perform mathematical operations on integers, i.e., you cannot perform calculations with decimals or floating-point numbers in Bash. To calculate the power of a decimal with an integer exponent, we will write the following Bash script:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/word-image-42.png" data-lazy- height="83" src="data:image/svg xml,” width=”800″>

In this Bash script, a one-liner echo command calculates the second power of “2.5.” The result is piped to the BC command, which will perform this calculation.

After saving this script, we can execute the following command:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/word-image-43.png" data-lazy- height="26" src="data:image/svg xml,” width=”352″>

The output of our Bash script results in a decimal number, as shown in the image below. This operation would not have been possible without the BC command.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/word-image-44.png" data-lazy- height="58" src="data:image/svg xml,” width=”371″>

Example 2: Checking Whether a Number is Less than Another Number

The BC command can also be used to compare if a number is greater or less than another. To draw such a comparison, we will write the following Bash script:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/word-image-45.png" data-lazy- height="85" src="data:image/svg xml,” width=”801″>

In this Bash script, again, a one-liner echo command is used. The command checks whether one number is less than another number. The result is piped to the BC command, which will perform this comparison. The output of this contrast will be “1” if the given statement is true; otherwise, the output will be “0.”

The output of the above Bash script is “0” since 10 is greater than 5, which makes our statement false. This output can be seen in the image below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/word-image-46.png" data-lazy- height="62" src="data:image/svg xml,” width=”378″>

Example 3: Checking Whether a Number Is Equal to Another Number

As in Example 2, the BC command is used again in this example; however, this time, the command will check whether one number is equal to another number. To draw such a comparison, we will write the following Bash script:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/word-image-47.png" data-lazy- height="85" src="data:image/svg xml,” width=”799″>

In this Bash script, a one-liner echo command checks whether the first number is equal to the other number. The result is piped to the BC command, which will perform this comparison. The output of this script will be “1” if the given statement is true; otherwise, the output will be “0” if the statement is false.

The output of our Bash script is “1” since 2 is equal to 2, which makes our statement true. This output can be seen in the image below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/word-image-48.png" data-lazy- height="55" src="data:image/svg xml,” width=”361″>

Example 4: Using the BC Command with the && Logical Operator

The BC command can also be paired up with logical operators in Bash, including && and ||, which correspond to logical multiplication and logical addition, respectively. The outcome of the && logical operator is true, or “1,” when all the provided inputs are non-zero. Otherwise, the result will be false, or “0.” To use the && operator with the BC command in Bash, we will write the following Bash script:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/word-image-49.png" data-lazy- height="84" src="data:image/svg xml,” width=”798″>

In this Bash script, a simple one-liner echo command performs the logical operation && between the numbers “10” and “0.” The result is piped to the BC command that will perform this operation.

The output of our Bash script is “0” since at least one of our provided values is not non-zero, which makes our operation false. This output can be seen in the image below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/word-image-50.png" data-lazy- height="57" src="data:image/svg xml,” width=”362″>

Example 5: Using the BC Command with the || Logical Operator

The result of the || logical operator is true, or “1,” when one of the provided inputs is non-zero. Otherwise, the result will be false, or “0.” To use the || operator with the BC command in Bash, we will write the following Bash script:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/word-image-51.png" data-lazy- height="86" src="data:image/svg xml,” width=”799″>

In this Bash script, a simple one-liner echo command performs the logical operation || between two numbers, “10” and “0.” The result is piped to the BC command that will perform this operation.

The output of our Bash script is “1” since one of our provided values is non-zero, which makes our operation true. This output can be seen in the image below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/word-image-52.png" data-lazy- height="59" src="data:image/svg xml,” width=”355″>

Example 6: Dividing Decimal Numbers with the Result in Decimal

We can also use the BC command to divide decimal numbers and return the result in decimal form, up to a specific decimal place. To obtain this decimal precision, we will write the following Bash script:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/word-image-53.png" data-lazy- height="88" src="data:image/svg xml,” width=”800″>

In this Bash script, a one-liner echo command divides two numbers, “6.5” and “2.7.” We want the result to be accurate to “3” decimal places, and we have used the “scale” flag for this purpose. The result is piped to the BC command that will perform this calculation.

The output of our Bash script results in a decimal number that is correct up to 3 decimal places, as shown in the image below. This would not have been possible without using the “scale flag” with the BC command. The output can be seen in the image below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/word-image-54.png" data-lazy- height="65" src="data:image/svg xml,” width=”371″>

Conclusion

In this article, we provided several examples of using the BC command in Bash script in Linux Mint 20. However, there is still a lot more that you can do with this powerful command that you can explore on your own and add more math and calculations to your bash scripts.

About the author

<img alt="Aqsa Yasin" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/Author-Image-150×150.jpg60396d8080761.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.