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

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 operators
  • Increment and decrements
  • Assignment
  • Comparison
  • Logical or Boolean operators
  • Math Functions
  • Conditional statements
  • iterative statements

Arithmetic operator Examples:

1. SUM / ADDITION of two or three numbers

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

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'.
5+5
10
5+5+6
16

2. subtracting two numbers

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

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
45
[root@TechTutorial ~]# echo "5*10*4" | bc
200

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
2
[root@TechTutorial ~]# echo "10/2" | bc
5

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
1.3333
[root@TechTutorial ~]# echo "scale=2;4/3" | bc
1.33

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
5

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
4
[root@TechTutorial ~]# echo "2^10" | bc
1024

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
15
[root@TechTutorial ~]# echo "var=10;var" | bc
10

The lists of assignment operators supported are:

  • var = value   : Assign the value to the variable
  • var += value : similar to var = var + value
  • var -= value  : similar to var = var – value
  • var *= value  : similar to var = var * value
  • var /= value   : similar to var = var / value
  • var ^= value  : similar to var = var ^ value
  • var %= value : similar to var = var % value

9. Increment Operator Examples:

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

  • ++var : Pre increment operator. The variable is incremented first and then the result of the variable is used.
  • var++ : Post increment operator. The result of the variable is used first and then the variable is incremented.
[root@TechTutorial ~]# echo "var=10;++var" | bc
11
[root@TechTutorial ~]# echo "var=10;var++" | bc
10

10. Decrements Operator Examples:

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

  • –var : Pre decrement operator. The variable is decremented first and then the result of the variable is used.
  • var– : Post decrement operator. The result of the variable is used first and then the variable is decremented.
[root@TechTutorial ~]# echo "var=10;--var" | bc
9
[root@TechTutorial ~]# echo "var=10;var--" | bc
10

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:

  • expr1 < expr2 : Result is 1 if expr1 is strictly less than expr2.
  • expr1 <= expr2 : Result is 1 if expr1 is less than or equal to expr2.
  • expr1 > expr2 : Result is 1 if expr1 is strictly greater than expr2.
  • expr1 >= expr2 : Result is 1 if expr1 is greater than or equal to expr2.
  • expr1 == expr2 : Result is 1 if expr1 is equal to expr2.
  • expr1 != expr2 : Result is 1 if expr1 is not equal to expr2.
[root@TechTutorial ~]# echo "10<=6" | bc
0
[root@TechTutorial ~]# echo "10<=12" | bc
1

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.

  • expr && expr: Result is 1 if both expressions are non-zero
  • expr || expr: Result is 1 if either expressions is non-zero.

below is the example

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

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:

  • Bc command treats the semicolon (;) or newline as the statement separator.
  • To group statements use the curly braces. Use with functions, if statement, for and while loops.
  • If only an expression is specified as a statement, then bc command evaluates the expression and prints the result on the standard output.
  • If an assignment operator is found. Bc command assigns the value to the variable and do not print the value on the terminal.
  • A function should be defined before calling it. Always the function definition should appear first before the calling statements.
  • If a standalone variable is found as a statement, bc command prints the value of the variable. You can also Use the print statement for displaying the list of values on the terminal.

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

Ankam Ravi Kumar

Working as Linux / Storage Administrator L3. Interested in sharing the knowledge.

Leave a Reply

Your email address will not be published.