Saturday, May 1, 2010

Computations in linux command line

The bc command is useful to perform some simple arithemetical calculations in console. It may be used in scripts too.

For example, percent values from my previous post have been obtained as

$> echo '265594/292891' | bc -l
$> echo '262938/292891' | bc -l

Argument of echo contains an arithmetic express, and echo pipes it to the bc with standard math library attached (-l).

Do you want to know the Euler's number e (the base of the natural logarithm)?

$> echo 'e(1)' | bc -l

Let's print first 80 digits of π (pi) using arctangent function (a(1.0)):

$> echo 'scale=80; 4.0*a(1.0)' | bc -l

The bc has interactive mode as well; just type

$> bc

In order to quit from interactive mode, type quit or exit.

No comments:

Post a Comment