Donate : Link
Medium Blog : Link
Applications : Link
bc command is used for command line calculator. It is similar to basic calculator by using which we can do basic mathematical calculations. Linux or Unix operating system provides the bc command and expr command for doing arithmetic calculations.
~/codeFactory$ bc --help
usage: bc [options] [file ...]
-h --help print this usage and exit
-i --interactive force interactive mode
-l --mathlib use the predefined math routines
-q --quiet don't print initial banner
-s --standard non-standard bc constructs are errors
-w --warn warn about non-standard bc constructs
-v --version print version information and exit
The bc command supports the following features:
- Arithmetic operators
- Increment or Decrement operators
- Assignment operators
- Comparison or Relational operators
- Logical or Boolean operators
- Math functions
- Conditional statements
- Iterative statements
1. Arithmetic operators
~/codeFactory$ echo "10+2" | bc
12
~/codeFactory$ echo "10*2" | bc
20
How to store the result of complete operation in variable?
~/codeFactory$ x=`echo "10/2" | bc`
~/codeFactory$ echo $x
5
2. Assignment Operators
The list of assignments 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
~/codeFactory$ echo "x=10;x" | bc
10
~/codeFactory$ echo "x=10;x*=2;x" | bc
20
3. Increment Operators
- ++var : Pre increment operator, variable is increased first and then result of variable is stored
- var++ : Post increment operator, result of the variable is used first and then variable is incremented.
~/codeFactory$ echo "x=10;++x" | bc
11
~/codeFactory$ echo "x=10;x++" | bc
10
4. Decrement Operators
- – – var : Pre decrement operator, variable is decreased first and then result of variable is stored.
- var – – : Post decrement operator, result of the variable is used first and then variable is decremented.
~/codeFactory$ echo "x=10;--x" | bc
9
~/codeFactory$ echo "x=10;x--" | bc
10
5. Comparison or Relational Operators
Relational operators are used to compare 2 numbers. If the comparison is true, then result is 1. Otherwise(false), returns 0.
- 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.
~/codeFactory$ echo "10<2" | bc
0
~/codeFactory$ echo "10==10" | bc
1
6. Logical or Boolean Operators
Logical 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 0 if expr is 1.
~/codeFactory$ echo "10 && 2" | bc
1
~/codeFactory$ echo "10>2 && 2<10" | bc
1
~/codeFactory$ echo "10>2 && 2>10" | bc
0
~/codeFactory$ echo "10>2 && 2||10" | bc
1
~/codeFactory$ echo "! 10>2" | bc
0
7. Mathematical Functions
Built-in math functions supported:
- s (x): The sine of x, x is in radians.
- c (x) : The cosine of x, x is in radians.
- a (x) : The arctangent of x, arctangent returns radians.
- l (x) : The natural logarithm of x.
- e (x) : The exponential function of raising e to the value x.
- j (n,x) : The bessel function of integer order n of x.
- sqrt(x) : Square root of the number x. If the expression is negative, a run time error is generated.
In addition to the math functions, the following functions are also supported:
- length(x) : returns the number of digits in x.
- read() : Reads the number from the standard input.
- scale(expression) : The value of the scale function is the number of digits after the decimal point in the expression.
- ibase and obase define the conversion base for input and output numbers. The default for both input and output is base 10.
- last (an extension) is a variable that has the value of the last printed number.
~/codeFactory$ pi=`echo "h=10;4*a(1)" | bc -l`
~/codeFactory$ echo $pi
3.14159265358979323844
~/codeFactory$ echo "scale($pi)" | bc -l
20
~/codeFactory$ echo "s($pi/3)" | bc -l
.86602540378443864675
~/codeFactory$ echo "c($pi/3)" | bc -l
.50000000000000000001
~/codeFactory$ echo "e(3)" | bc -l
20.08553692318766774092
~/codeFactory$ echo "l(e(1))" | bc -l
.99999999999999999999
~/codeFactory$ echo "obase=2;15" | bc -l
1111
~/codeFactory$ echo "obase=8;9" | bc -l
11
~/codeFactory$ echo "ibase=2;1111" | bc -l
15
~/codeFactory$ echo "ibase=2;obase=8;10" | bc -l
2
8. Conditional Statements
Conditional Statements are used to take decisions and execute statements based on these decisions.
~/codeFactory$ echo 'n=2;m=10;if(n>m) print "n is greater" else print "m is greater" ' | bc -l
m is greater
9. Iterative statements
~/codeFactory$ echo "for(i=0; i<5; i++) {i;}" | bc
0
1
2
3
4
~/codeFactory$ echo "i=0;while(i<5) {i; i+=1}" | bc
0
1
2
3
4
Some Pseudo Statements:
- break : This statement causes a forced exit of the most recent enclosing while statement or for statement.
- continue : The continue statement (an extension) causes the most recent enclosing for statement to start the next iteration.
- halt : The halt statement (an extension) is an executed statement that causes the bc processor to quit only when it is executed. For example, “if (0 == 1) halt” will not cause bc to terminate because the halt is not executed.
- return : Return the value 0 from a function. (See the section on functions).
- return(expression) : Return the value of the expression from a function. (See the section on functions). As an extension, the parenthesis are not required.
- limits : Print the local limits enforced by the local version of bc. (This is an extension).
- quit : When the quit statement is read, the bc processor is terminated, regardless of where the quit statement is found. For example, “if (0 == 1) quit” will cause bc to terminate.
- warranty : Print a warranty notice. (This is an extension).
10. Functions
Functions provide a method of defining a computation that can be executed later. Functions in bc always compute a value and return it to the caller. Function definitions are “dynamic” in the sense that a function is undefined until a definition is encountered in the input. That definition is then used until another definition function for the same name is encountered. The new definition then replaces the older definition.
define name (parameters)
{
statements.......
.......
........
return statement
}
11. We can write our arithmetic expressions in a file and then execute those statements by providing the filename to the bc command.
~/codeFactory$ cat test3.txt
2+10;
var = 10*2
var
print var
quit
~/codeFactory$ bc test3.txt
bc 1.07.1
12
20
20
12. 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.

