Site icon ARKIT

bc command 10 practical examples Shell Scripting Tutorial Arithmetic Operators using bc

bc command 10 examples

bc command 10 examples

bc command 10 practical examples

Most of times for arithmetic operations we use expr command for doing arithmetic calculations. Instead of expr command we can also use ‘bc’ command to do arithmetic calculations. You can use these commands in bash or shell script also for evaluating arithmetic expressions.  bc command

In this article we are going to discuss about bc command, what are the operations it can do and its advantages.

bc command can do below things

Arithmetic operator Examples:

1. SUM / ADDITION of two or three numbers

[root@TechTutorial ~]# echo "5+5" | bc
[root@TechTutorial ~]# echo "5+5+6" | bc

As shown in above example bc command is capable to doing SUM of given numbers. We can also use bc command in different way as shown below. In terminal type ‘bc’ then it will enter into the bc then without giving  any extra characters type your required arithmetic operation.

[root@TechTutorial ~]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.

2. subtracting two numbers

[root@TechTutorial ~]# echo "20-6" | bc

by giving ‘-‘ (minus / ifan )  in between the numbers / values it will subtract and provide you the result.

3. Multiplying two Or three Numbers

we can multiply the numbers using bc command, followed by ‘*’ (star) operator, in between the numbers. Lets see the below examples.

[root@TechTutorial ~]# echo "5*9" | bc
[root@TechTutorial ~]# echo "5*10*4" | bc

4. Dividing two numbers

bc command will divide given numbers by ‘/’ (slash) operator. When you divide the numbers using divide operator (slash) it will not print the decimal numbers after DOT. value. It will only gives you value without decimal values.

[root@TechTutorial ~]# echo "10/5" | bc
[root@TechTutorial ~]# echo "10/2" | bc

5. Dividing two numbers with decimal values

Using scale function along with bc command it will provide you the decimal values.

[root@TechTutorial ~]# echo "scale=4;4/3" | bc
[root@TechTutorial ~]# echo "scale=2;4/3" | bc

As shown example above  scale=2 will provided you the 2 decimal numbers and scale=4 provided 4 decimal values. If you want an accurate value with the divide  operator use scale function.

6. Modulus Operator and remainder of after an integer division

[root@TechTutorial ~]# echo "5%6" | bc

7. Raise to the power  / power of

Using an exponent operator we can achieve 2 to the power of 2 value using bc command. Lets see the example below

[root@TechTutorial ~]# echo "2^2" | bc
[root@TechTutorial ~]# echo "2^10" | bc

8. Assignment Operator Examples:

Assignment operator are used to assign a value to the variable. The following example shows how to use the assignment operators:

[root@TechTutorial ~]# echo "var=10; var+=5;var" |bc
[root@TechTutorial ~]# echo "var=10;var" | bc

The lists of assignment operators supported are:

9. Increment Operator Examples:

There are two kinds of increment operators. They are pre increment and post increment operators.

[root@TechTutorial ~]# echo "var=10;++var" | bc
[root@TechTutorial ~]# echo "var=10;var++" | bc

10. Decrements Operator Examples:

Similar to the increment operators, there are two types of decrements operators.

[root@TechTutorial ~]# echo "var=10;--var" | bc
[root@TechTutorial ~]# echo "var=10;var--" | bc

Relational Operators Examples:

Relational operators are used to compare two numbers. If the comparison is true, then it returns 1. Otherwise (false), it returns 0. The relational operators are mostly used in conditional statements like if. The list of relational operators supported in bc command are shown below:

[root@TechTutorial ~]# echo "10<=6" | bc
[root@TechTutorial ~]# echo "10<=12" | bc

Logical Operator Examples:

Logical operator are also mostly used in conditional statements. The result of the logical operator is either 1 (true) or 0 (false) ! expr: Result is 1 if expr is 0.

below is the example

[root@TechTutorial ~]# echo "10 && 11" | bc
[root@TechTutorial ~]# echo "10 || 11" | bc
[root@TechTutorial ~]# echo "0 || 0" | bc

Conditional statements

Conditional statements are used to decisions and execute statements based on these decisions. bc command supports the if condition. The following example shows to use the if condition

[root@TechTutorial ~]# echo  'if(1 == 2) print "true" else print "false"' | bc
false[root@TechTutorial ~]# echo  'if(1 == 1) print "true" else print "false"' | bc
true[root@TechTutorial ~]#

Important Points:

Please Provide your Valuable Comments about this article.

Related Articles

Shell Scripting Training Hyderabad Course Content

Shell Scripting Building Blocks

Download Free Shell Scripting Books and Learn your own

Thanks for your wonderful Support and Encouragement