The “bc” Command in Linux [10+ Practical Examples]

The bc command in Linux is a command line calculator. It is the short form of basic calculator. We can do basic mathematical calculations by using the bc command in Linux. The bc command and expression are available in the Linux or Unix operating system for performing mathematical operations. These commands can evaluate arithmetic expressions in shell script or bash.

A. Description

The bc command is a command line calculator that is very convenient for all levels of users. This is a convenient tool for doing mathematical operations swiftly.

B. Syntax

The bc command in Linux is a built-in command that takes OPTIONS and FILE as its argument. The general syntax for using bc commands is:

bc [ -hlwsqv ] [LONG-OPTIONS] [ FILE]...

Note: In the above syntax, the OPTIONS and FILE are enclosed by a square bracket which means that OPTIONS and FILE are not mandatory for the command.

C. Options

One or more options can be added to the syntax of the bc command to modify the command. I have listed some useful options below. If you do not find your desired option here, you will find it on the man (manual) page. To go man page, type the following command and press ENTER.

man bc

Useful Options

  • -h/–help (Print the usage and exit).
  • -i/–interactive (Force interactive mode.)
  • -l/–mathlib (Define the standard math library.)
  • -w/–warn (Give warnings for extensions to POSIX bc.)
  • -s/–standard (Process exactly the POSIX bc language.)
  • -q/–quiet (Do not print the normal GNU bc welcome.)
  • -v/–version (Print the version number and copyright and quit.)

Note: All options are case-sensitive. You must be careful while using these.

Practical Examples of the “bc” Command in Linux

The bc (basic calculator) command in Linux may perform useful operations like converting integers to hexadecimal, binary, and vice versa, or handle algebra and variables. Going through these examples, you will learn how to use the bc command on Linux & you’ll certainly notice that the syntax for the bc is pretty similar to that of the C programming language. A few real-life examples of the bc command are given below.

Example 1: Arithmetic Operation Using “bc” Command in Linux

The bc command in Linux is a swift tool for doing arithmetic operations. Here I will do the arithmetic operation of 2 multiplied by 3. To do so, follow the procedures below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

echo 2*3 | bc

➌ Now, press the ENTER button.

Output >

I utilized an arithmetic operation(multiplication) by employing the bc command in Linux, as depicted in the attached image.arithmetic multiplication operation is done by employing the bc command in Linux


Similar Readings


Example 2: Assignment Operation Using “bc” Command in Linux

The bc command in Linux is a swift tool for doing assignment operations. A list of assignment operations supported is given below.

Command Output
echo “var=10;var” | bc 10
echo “var=2;var+=10;var” | bc 12
echo “var=20;var-=5;var” | bc 15
echo “var=15;var*=5;var” | bc 75
echo “var=15;var/=5;var” | bc 3
echo “var=10;var%=3;var” | bc 1

Here, I will keep the value of var equal to 10, then increase the power of var by two and keep the resultant at var again with the help of the assignment operation. To do so, follow the procedures below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

echo “var=10;var^=2;var” | bc

➌ Now, hit the ENTER button.

Output >

The image demonstrates my execution of the assignment operation via the bc command.assignment operation is executed via the bc command in Linux.

Example 3: Increment Operation Using “bc” Command in Linux

You can use The bc command in Linux for doing increment operations. There are two kinds of increment operators: (a) ++var: Pre increment operator, the variable is increased initially, and then the result of the variable is preserved. (b) var++: Post increment operator, the result of the variable is used initially, and then the variable is incremented.

Case A: Pre-increment Operation

Here, I will keep the value of var equal to 10, then increase the value of var by one with the help of the bc command and print the resultant with the help of the echo command. To do so, follow the procedures below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

echo “var=10;++var” | bc

➌ Now, execute the ENTER button.

Output >

As the image illustrates, I have implemented the pre-increment operation using the bc command.pre-increment operation is implemented using the bc command in Linux

Case B: Post-Increment Operation

Here, I will keep the value of var equal to 10. Firstly print the original value of var with the help of the echo command, then increase the value of var by one with the help of the bc command and preserve. To do so, follow the procedures below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

echo “var=10;var++” | bc

➌ Now, execute the ENTER button.

Output >

The image illustrates my execution of the post-increment operation with the assistance of the bc command.post-increment operation is done with the assistance of the bc command in Linux.

Note: Here, value 10 of var is printed first with the help of echo, then the value of var is increased by 1, and finally, the new value of var, which is 11, is preserved in var.

Example 4: Decrement Operation Using “bc” Command in Linux

You can use The bc command in Linux for increment operations. There are two kinds of decrement operators: (a) –var: In pre decrement operator, the variable is decreased first, and then the result of the variable is stored. (b) var–: Post decrement operator, the result of the variable is used first, and then the variable is decremented.

Case A: Pre-Decrement Operation

Here, I will keep the value of var equal to 10, then decrease the value of var by one with the help of the bc command and print the resultant with the help of the echo command. To do so, follow the procedures below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

echo “var=10;--var” | bc

➌ Now, execute the ENTER button.

Output >

The attached image showcases my execution of the pre-decrement operation through the bc command.pre-decrement operation is executed through the bc command.

Case B: Post-Decrement Operation

Here, I will keep the value of var equal to 10. Firstly print the original value of var with the help of the echo command, then decrease the value of var by one and preserve with the help of the bc command. To do so, follow the procedures below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

echo “var=10;var--” | bc

➌ Now, push the ENTER button.

Output >

The bc command is employed to carry out the post-decrement operation, as the image displays.The bc command is employed to carry out the post-decrement operation

Note: Here, value 10 of var is printed first with the help of echo, then the value of var is decreased by 1, and finally, the new value of var, which is 9, is stored in var.

Example 5: Comparison of Relational Operation Using “bc” Command in Linux

You can easily perform compare or do relational operations on multiple numbers using the bc command in Linux. Relational operators are used to comparing two numbers. If the comparison is true, then the result is 1. Otherwise(false), returns 0. These operators are generally used in conditional statements like if. The list of relational operators supported in the bc command is shown below:

Syntax Output
expression1<expression2 Result is 1 if expression1 is less than expression2.
expression1<=expression2 Result is 1 if expression1 is less than or equal to expression2.
expression1>expression2 Result is 1 if expression1 is strictly greater than expression2.
expression1>=expression2 Result is 1 if expression1 is greater than or equal to expression2.
expression1==expression2 Result is 1 if expression1 is equal to expression2.
expression1!=expression2 Result is 1 if expression1 is not equal to expression2.

Here, I will compare 10 with 4. To do so, follow the procedures below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

echo “10>5” | bc

➌ Now, push the ENTER button.

Output >

The following image displays my implementation of comparison or relational operation using the bc command.comparison or relational operation is implemented using the bc command.

Example 6: Logical or Boolean Operation Using “bc” Command in Linux

You can use the bc command in Linux for logical or boolean operations. Logical operators are mostly used in conditional statements. The result of the logical operators is either 1(TRUE) or 0(FALSE). The list of boolean operations and their output are given below.

Syntax Output
Boolean “and” operation: expression1 && expr2 Result is 1 if both expressions are non-zero
Boolean “or” operation: expr1 || expr2 Result is 1 if either expression is non-zero.
Boolean “not” operation: !expression Result is 1 if the expression is 0.

Here, I will perform the boolean “or” operation of 2 and 3. To do so, follow the procedures below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

echo “2&&3” | bc

➌ Now, hit the ENTER button.

Output >

The following image depicts that I have performed the logical or boolean operation (in this case, boolean and operation) using the bc command.boolean and operation has been performed using the bc command.

Example 7: Conditional Statements Using “bc” Command in Linux

The bc command in Linux is a handy tool for operating conditional statements. Conditional Statements are used to make decisions and execute statements based on these decisions. The bc command supports the if condition.  Here I will compare m and n (where m is 99, and n is 88) with the help of the bc command. To do so, follow the procedures below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

echo ‘n=88;m=99;if(n>m) print "n is greater" else print "m is greater" ‘ | bc -l

➌ Now, hit the ENTER button.

Output >

The bc command is utilized to execute the conditional statement, as depicted in the following image.The bc command is utilized to execute the conditional statement


Similar Readings


Example 8:  Converting from One Number System to Another One Using “bc” Command in Linux

You can easily convert numbers from one number system to another using the bc command in Linux. You have to put the base of the output number in the obase and the base of the input number in the ibase.

Case A: Convert a Decimal Number to a Binary Number

To convert a decimal number to a binary number, you have to put 2 as obase(output) and 10 as ibase(input). Here I will convert the decimal number 7 to a binary number with the help of the bc command. To do so, follow the procedures below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

echo "obase=2; ibase=10; 7;" | bc

➌ Now, activate the ENTER button.

Output >

The following image shows that I have converted a decimal number 7 to a binary number 111 using the bc command.A decimal number 7 has been converted to a binary number 111 using the bc command.

Case B: Convert a Binary Number to a Decimal Number

To convert a binary number to a decimal number, you have to put 10 as obase(output) and 2 as ibase(input). Here I will convert the binary number 101 to a decimal number 5 with the help of the bc command. To do so, follow the procedures below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

echo "obase=10; ibase=2; 101;" | bc

➌ Now, activate the ENTER button.

Output >

The following image shows that I have converted a binary number 101 to a decimal number 5 using the bc command.A binary number 101 has been converted to a decimal number 5 using the bc command.

Example 9:  Executing Mathematical Operations from a File Using “bc” Command in Linux

You can execute a mathematical operation from a file using the bc command in Linux. Here, I will execute some mathematical operations from a file named sample.txt. To do so, follow the procedures below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

nano sample.txt

➌ Now, push the ENTER button.

➍ Type the following text lines

2+5;

var = 10*3

print var

quit

➎ Now push CTRL+S then CTRL+X

➏ Now type the following command

bc -q sample.txt

➐ Now, push the ENTER button.

Output >

As shown in the following image, I have utilized the bc command to perform a mathematical operation from a file named sample.txt.The bc command is utilized to perform a mathematical operation from a file named sample.txt.

Example 10: Using Mathematical Functions of Linux

There are many built-in functions in Linux that can be utilized with the bc command. Here I will use a mathematical function named a(arctangent). To do so, follow the procedures below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

pi =`echo"h=10; 4*a(1)" | bc -l`

Now, This command will set the value of the area of a circle having a radius of 1 to the variable pi which is almost 3.1416. Here, option -l tells the bc command to use the math library function.

➌ Now, hit the ENTER button.

➍ Type the following command in the command prompt:

echo $pi

➎ Now, hit the ENTER button.

Output >

The image displays my execution of a mathematical function with the help of the bc command.A mathematical function is executed with the help of the bc command.

Conclusion

In this article, I have tried to demonstrate the applications and effectiveness of the bc command in Linux. I hope after going through this article, you’ll be competent enough to explore more things with the help of the illustrated practical examples.


Similar Readings

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment