bc is a command line utility in Linux distributions which can perform arbitrary precision calculations. In this article we will discuss bc command all options with examples in Linux. With bc command we can do simple to complex calculations in Linux distributions. bc is a language that supports arbitrary precision numbers with interactive execution of statements.
A standard math library is available by command line option. If requested, the math library is defined before processing any files. bc starts by processing code from all the files listed on the command line in the order listed. After all files have been processed, bc reads from the standard input. All code is executed as it is read. If a file contains a command to halt the processor, bc will never read from the standard input.
BC command options:
How to print the all options available in bc command:
To list the all options available in bc command use -h or –help option as below.
$ bc -h
$ bc --help
How to open bc command utility in interactive mode:
By default bc command work on interactive mode. If it is not starting in interactive mode otherwise you want to use interactive mode use -i or –interactive options as below.
$ bc -i
$ bc --interactive
$ bc -i
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
How to use mathematical functions in bc command:
bc command support use of mathematical functions to evaluate the expressions. With -l or –mathlib options we can use mathematical functions.
$ echo "scale=10; a(2)" | bc -l
$ echo "scale=10; a(2)" | bc --mathlib
$ echo "scale=10; a(2)" | bc -l
1.1071487177
Above command will evaluate 2 with mathematical function a (means the arctangent of 2) by adding 10 decimal values as we given 10 in scale.
Donot print the welcome message in interactive mode:
In interactive mode of bc command, it will display the welcome message saying bc version, copyright details and warranty details. If you don’t want to print those details we can use -q or –quiet option to skip those welcome messages.
bc command with welcome message in interactive mode.
$ bc
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
bc command without welcome message
$ bc -q
$ bc --quiet
How to check the version of bc command installed in Linux server:
If you open bc utility in interactive mode, version details will be displayed by default. If you want to check explicitly the bc command version currently installed in Linux use -v or –version option as below.
$ bc -v
$ bc --version
See also:
- cat command all options with examples in Linux
- Linux Filesystem Hierarchy Structure
- ls command all options with examples in Linux
- mkdir command all options with examples in Linux
- Shell script for ls command examples in Linux
- df – disk free command all options with examples in Linux
- Shell script for df – disk free command examples in Linux
- du – disk usage command all options with examples in Linux
- Shell script for du – disk usage command examples in Linux
- alias command all options with examples in Linux
BC command elements:
As bc is a language that supports arbitrary precision numbers with interactive execution of statements, we have few built in types in bc language which can used in bc command to evaluate the calculations. Please find the built in types and default elements of bc language below.
Numbers:
The most basic element in bc is the number. Numbers are arbitrary precision numbers. This precision is both in the integer part and the fractional part. All numbers are represented internally in decimal and all computation is done in decimal.
Variables:
Numbers are stored in two types of variables, simple variables and arrays. Both simple variables and array variables are named. Names begin with a letter followed by any number of letters, digits and underscores. All letters must be lower case. There are four special variables, scale, ibase, obase, and last.
Comments:
Comments in bc start with the characters /* and end with the characters */. Comments may start anywhere and appear as a single space in the input. A single line comment starts at a # character and continues to the next end of the line. The end of line character is not part of the comment and is processed normally.
Expressions:
The numbers are manipulated by expressions and statements. There is no “main” program. Instead, code is executed as it is encountered. A simple expression is just a constant. bc converts constants into internal decimal numbers using the current input base, specified by the variable ibase. The legal values of ibase are 2 through 36.
Statements:
Statements provide the sequencing of expression evaluation. Execution happens when a newline in encountered and there is one or more complete statements. Due to this immediate execution, newlines are very important in bc. In fact, both a semicolon and a newline are used as statement separators. An improperly placed newline will cause a syntax error. Because newlines are statement separators, it is possible to hide a newline by using the backslash character.
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. A function is defined as follows:
define test_functions(parameters)
{
Program statements
}
To call the function use the function name with parameters to that function if any as below.
test_function(parameters)
Math library:
We can use in built math library functions in bc command with -l or –mathlib option. Please find the below list of available mathematical functions and their descriptions used in bc command.
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. Math library functions in bc command |
Examples of bc command calculations:
bc command can be used in three ways. One is pass the bc command to an expression using pipe. Second is use bc command in a interactive mode. Third one is pass the file containing the expressions to bc command.
How to pass bc command for expression:
$ echo "10+40" | bc
$ echo "5*4" | bc
$ echo "3^2" | bc
How to use bc command in interactive mode:
$ bc
10+40
5*4
3^2
How to calculate the expression reading from file:
We have some calculations mentioned in the file data.txt as below.
$ cat data.txt
140+30-10*5
10/5*3-20
quit
Execute the bc command with above file name as input to perform the calculations and print the output.
$ bc data.txt
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
120
-14
How to perform arithmetic operations using bc command:
Use + operator as below to add two numbers:
$ echo "10+40" | bc
50
Use – operator as below to subtract two numbers:
$ echo "55-10" | bc
45
Use / operator as below to divide two numbers:
$ echo "40/10" | bc
4
Use * operator as below to multiply two numbers:
$ echo "10*3" | bc
30
Use % operator as below for modulus operation.
$ echo "40%10" | bc
0
How to increase and decrease numbers using bc command:
There are two types of increment operators available to increase and decrease the numbers. They are ++ (increment) and –(decrement)
If we add ++ before the variable, it is called pre increment. Here first value of the number will be increased by 1 and assign the incremented value to the variable
$ echo "x=10; ++x;" | bc
11
If we add ++ after the variable, it is called post increment. Here first number will be assigned to the variable and value will be increased by 1.
$ echo "x=10; x++; x" | bc
10
11
If we add — before the variable, it is called pre decrement. Here first value of the number will be decreased by 1 and assign the decreased value to the variable
$ echo "x=10; --x;" | bc
9
If we add — after the variable, it is called post decrement. Here first number will be assigned to the variable and value will be decreased by 1.
$ echo "x=10; x--; x" | bc
10
9
How to perform assignment operations using bc command:
By using =, +=, -=, *=, /=, ^=, %= assignment operators we can perform assignment operations using bc commands.
Using ‘=’ operator:
$ echo "x=20; x" | bc
20
Using ‘+=’ operator:
$ echo "x=10; x+=15; x" | bc
25
Using ‘-=’ operator:
$ echo "x=45; x-=5; x" | bc
40
Using ‘*=’ operator:
$ echo "x=45; x*=5; x" | bc
225
Using ‘/=’ operator:
$ echo "x=45; x/=5; x" | bc
9
Using ‘^=’ operator:
$ echo "x=10; x^=5; x" | bc
100000
Using ‘%=’ operator:
$ echo "x=10; x%=20; x" | bc
10
How to use comparison and or relational operators in bc command:
We can use comparison or relational operators like <, <=, >, >=, ==, != as mentioned below. With conditional operators the output will be in 1s and 0s format. If the condition is true output is 1 and if condition is false output is 0.
Using ‘<’ operator:
$ echo "x=10; y=40; x<y" | bc
1
$ echo “20>50” | bc
0
Using ‘<=’ operator:
$ echo "x=10; y=40; x<=y" | bc
1
Using ‘>’ operator:
$ echo "x=10; y=40; x>y" | bc
0
Using ‘>=’ operator:
$ echo "x=50; y=40; x>=y" | bc
1
Using ‘==’ operator:
$ echo "x=10; y=40; x==y" | bc
0
Using ‘!=’ operator:
$ echo "x=10; y=40; x!=y" | bc
1
How to use logical or Boolean operators in bc command:
Logical operators are used to compare two or more expressions in conditional statements and return true or false. 1 is for true and 0 is for false. There are 3 types of logical operators available. They are && (logical and), || (logical or), ! (logical not).
Using ‘&&’ operator:
$ echo "x=10; x && y" | bc
0
using ‘||’ operator:
$ echo "x=10; x || y" | bc
1
Math functions examples in bc command:
By using math functions we can calculate arctangent, sine, cosine trigonometric functions for numbers.
How to use arctangent math function in bc command:
Below are the math functions available in bc command.
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.
We can calculate pi value by using arctangent math function in bc command as follows.
$ echo "h=10;4*a(1)" | bc -l
3.14159265358979323844
How to use Sine math function in bc command:
To calculate sine value for 5 use below command.
$ echo "s(5)" | bc -l
-.95892427466313846889
If you want to limit the number of decimal places in the output use scale function with number of decimal points required as below.
$ echo "scale=3; s(5)" | bc -l
-.958
How to use Cosine math function in bc command:
To calculate cosine value for 5 use below command.
$ echo "c(5)" | bc -l
.28366218546322626446
To limit number of decimal points
$ echo "scale=3; c(5)" | bc -l
.282
How to find the natural logarithm value for 5 using bc command:
$ echo "l(5)" | bc -l
1.60943791243410037460
To limit number of decimal points
$ echo "scale=3; l(5)" | bc -l
1.609
How to find the exponential value of 5 using bc command:
$ echo "e(5)" | bc -l
148.41315910257660342111
To limit number of decimal points
$ echo "scale=3; e(5)" | bc -l
148.413
How to use conditional statements in bc command:
We can use ‘if’ conditional statements in bc command. Please find the syntax below.
$ if(condition) {statements in if block} else {statements in else block}
$ echo 'x=10; y=45; if(x < y && x >= 5) print "x is greater than y" else print "y is greater than x" ' | bc -l
x is greater than y
$ echo 'x=10; y=45; if(x > y || x <= 5) print "x is greater than y" else print "y is greater than x" ' | bc -l
y is greater than x
How to use Iterative statements in bc command:
Iterative statements like for, while can be supported in bc command. Syntax used in ‘for’, ‘while’ is same as in any other programming language. Please find the examples below.
For loop example:
$ echo "for(i=1; i<=10; i++) {i}" | bc
1
2
3
4
5
6
7
8
9
10
While loop example:
$ echo "i=1; while(i<=10) {i; i+=1}" | bc
1
2
3
4
5
6
7
8
9
10
See also:
- b2sum command all options with examples in Linux
- How to encode and decode data using base32 and base64 commands in Linux
- How to print filename or basename from a path in Linux
- What is bash (Bourne again shell). A detailed guide about bash options with examples in Linux
- How to schedule a job in Linux. at, atq, atrm, batch commands all options with examples in Linux
For more detailed information about the bc command and its options please refer manual for bc command.