bc command is used for the command line calculator. It is similar to basic calculator by using which we can perform
mathematical calculation. bc command supports the following operators:
- Arithmetic Operators
- Assignment Operator
- Increment/Decrement Operators
- Relational Operator
- Logical or Boolean Operators
- Math Functions
- Conditional Statements
- Iterative Statements
bc supports addition (+), subtraction (-), multiplication (*), division (/), remainder (%) and rxponentiation (^) operators.
$ echo "12+5" | bc
17
$ echo "12-5" | bc
7
$ echo "12*5" | bc
60
$ echo "19/5" | bc
3
$ echo "19%5" | bc
4
$ echo "2^5" | bc
32We can perform these operations on variables.
$ a=5
$ b=2
$ echo "$a+$b" | bc
7
$ echo "$a-$b" | bc
3
$ echo "$a*$b" | bc
10
$ echo "$a/$b" | bc
2
$ echo "$a%$b" | bc
1
$ echo "$a^$b" | bc
25
we can also store the result of above operations in variable.
$ res=$(echo "10*5" | bc)
$ echo "$res"
50The list of assignments operators supported are:
var = valueassign the vale to the variablevar += valuesimilar tovar = var + valuevar -= valuesimilar tovar = var – valuevar *= valuesimilar tovar = var * valuevar /= valuesimilar tovar = var / valuevar ^= valuesimilar tovar = var ^ valuevar %= valuesimilar tovar = var % value
# Assign 10 to the variable and print the value on the terminal.
$ echo "var=10; var" | bc
10
$ echo "var=10; var ^= 2; var" | bc
100
$ x=$(echo "var=500; var%=7; var" | bc)
$ echo $x
3
- Pre increment operator
(++var)Variable is increased first and then result of variable is stored. - Post Increment Operator
(var++)Result of the variable is used first and then variable is incremented. - Pre Decrement operator
(--var)Variable is decreased first and then result of variable is stored. - Post Decrement operator
(var--)Result of the variable is used first and then variable is decremented.
$ echo "var = 10; var--" | bc
10
$ echo "var = 10; ++var" | bc
11
$ echo "var = 10; var++" | bc
10
$ echo "var = 10; --var" | bc
9Relational operators are used to compare 2 numbers. If the comparison is true, then result is 1. Otherwise(false), returns 0. These operators are generally used in conditional statements like if. The list of relational operators supported in bc command are shown below:
expr1 < expr2: Result is 1 ifexpr1is strictly less thanexpr2.expr1 <= expr2: Result is 1 ifexpr1is less than or equal toexpr2.expr1 > expr2: Result is 1 ifexpr1is strictly greater thanexpr2.expr1 >= expr2: Result is 1 ifexpr1is greater than or equal toexpr2.expr1 == expr2: Result is 1 ifexpr1is equal toexpr2.expr1 != expr2: Result is 1 ifexpr1is not equal toexpr2.
$ echo "10 > 5" | bc
1
$ echo "10 == 5" | bc
0
$ echo "1 >= 1" | bc
1Logical operators are mostly used in conditional statements. The result of the logical operators is either 1(TRUE)
or 0(FALSE).
expr1 && expr2: Result is 1 if both expressions are non-zero.expr1 || expr2: Result is 1 if either expression is non-zero.!expr: Result is 1 if expr is 0.
$ echo "10 && 5" | bc
1
$ echo "0 || 0" | bc
0
$ echo "! 0" | bc
1- use
–lto import the standard math library. sqrt(x)returns the square root of the number x. If the expression is negative, a run time error is generated.length(x)returns the number of digits in x.ibaseandobasedefines the conversion base for input and output numbers. The default for both input and output is base 10.scaledefines number of digits after the decimal point in the expression.lastis a variable that has the value of the last printed number.
$ echo "obase=2; 15" | bc –l
1111
$ echo "obase=8; 9" | bc –l
11
$ echo "ibase=2; 1111" | bc –l
15
$ echo "ibase=2; obase=8; 10" | bc –l
2
Conditional Statements are used to take decisions and execute statements based on these decisions. bc
command supports the if condition.
if(condition) {statements} else {statemnts}To test a given number is even or odd:
$ echo 'n=8; if(n%2==0) print "n is even\n" else print "n is odd\n" ' | bc
n is even
$ echo 'n=9; if(n%2==0) print "n is even\n" else print "n is odd\n" ' | bc
n is odd
bc command supports the for loop and while loop for doing iterations.
for(assignment; condition; updation)
{
statements.....
.......
........
}
OR
while(condition)
{
statements.....
.......
........
} $ echo "for(i=1; i<=5; i++) {i;}" | bc
1
2
3
4
5
$ echo "i=1;while(i<=5) {i; i+=1}" | bc
1
2
3
4
5The expression precedence from lowest to highest is as follows:
||operator, left associative&&operator, left associative!operator, nonassociativeRelationaloperators, left associativeAssignmentoperator, right associative+and-operators, left associative*,/and%operators, left associative^operator, right associative- unary
-operator, nonassociative ++and--operators, nonassociative