bc Command in Linux with Examples

This tutorial explains Linux “bc” command, options and its usage with examples.

bc – An arbitrary precision calculator language

DESCRIPTION

The bc command is an interactive process that provides arbitrary-precision arithmetic. The bc command first reads any input files specified by the File parameter and then reads the standard input. The input files must be text files containing a sequence of commands, statements, or function definitions that the bc command can read and execute.

bc utility is surely one of the underdogs when it comes to calculations on command line.

SYNOPSIS

bc [-c] [-l] [file]


OPTIONS

advertisement
advertisement

-c
Compile only. The output is dc commands that are sent to the standard output.
-l
Define the math functions and initialize scale to 20, instead of the default zero.
file
Name of the file that contains the bc commands to be calculated this is not a necessary command.

USAGE

L
A letter a-z
E
An expression: a (mathematical or logical) value, an operand that takes a value, or a combination of operands and operators that evaluates to a value.
S
Statement
/* and /*
Comment
sqrt ( E )
Square root
length ( E )
Number of significant decimal digits.
scale ( E )
Number of digits right of decimal point.
quit
Exits the bc command.

Math Functions:

The built-in math functions supported are:

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.

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.

Available Operators

advertisement

+ – * / % ^
(% is remainder; ^ is power)

++ — (prefix and postfix; apply to names)

== = != = =+ =- =* =/ =% =^

Functions:

A function is a code block which executes logically related functionality and returns a value. The syntax of creating a function is

advertisement
define function-name(comma separated parameters list) {
statements
return statement
}

Iterative Statements:

Bc command supports the for and while loop for doing iterations. The syntax of for and while loop are shown below:

for (assignment; condition; increment) {
statements
}
 
while (condition) {
statements
}

Conditional Statement:

Conditional statements are used to take decisions and execute statements based on these decisions. Bc command supports the if condition. The syntax of if statement is

if(condition) { statements} else {statements}

EXAMPLES

1. Basic calculations

$ bc <<< 5*4
20
$ bc <<< 5+4
9

There is another way of doing the same by using echo command.

Here are some examples:

$ echo "5*4" | bc
20
$ echo "5+4" | bc
9
$ echo "5-4" | bc
1

2. Calculations of expression in file by directing the file to bc

$ cat calcFile 
5+5
6+7
 
$ bc < calcFile 
10
13

3. Calculating square root and concept of scale

$ echo "sqrt(10)" | bc
3
$ echo "scale=1;sqrt(10)" | bc
3.1
$ echo "scale=10;sqrt(10)" | bc
3.1622776601

We see that the value assigned to variable ‘scale’ decides the number of post decimal digits. If ‘scale’ is not specified then number of post decimal digits are taken as 0.

4. Variables and bc

$ x=`echo "5-4" | bc`
$ echo $x
1

So we see that by using back ticks you can store the result in a variable.

5. Base Conversion

-> Convert decimal to hexadecimal

$ echo 'obase=16;128' | bc
80

-> Convert hexadecimal to decimal

$ echo 'ibase=16;obase=A;80' | bc
128

-> Convert decimal to binary

$ echo 'obase=2;128' | bc
10000000

-> Convert binary to decimal

$ echo 'ibase=2;obase=A;10000000' | bc
128

6. Conditional Statement Example

$ echo 'if(1 == 2) print "true" else print "false"' | bc
false

7. Example of functions and iterative statements

$cat arth_expr.dat
2+5;
var = 10*3
var
print var
define sum(a,b) {
return a+b
}
sum(3,5)
quit
 
$ bc arth_expr.dat
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
7
30
30
8

Be default the bc command prints the welcome message(version, copyright message. You can suppress this welcome message by using the -q option with bc command

$ bc -q arth_expr.dat

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.