bc Extensions

More often than not, if I have to do any calculations from my laptop, I eschew the very fine GUI application PCalc in favor of bc on the command line. I also like to use the extended version, bc -l to get decimal places in my result.

Like most bc enthusiasts, I have this aliased in my .bash_profile file like so:

alias bc='bc -l'

But if you’re a bc nerd, this probably isn’t enough; you also want to pre-define functions and have a bunch of scientific constants, too. There’s a pretty standard extensions.bc file out there, and you can get it from the straight from the open-source project: first a buncha functions, then the scientific constants.

The usual way is to include these when you run bc, so your alias in .bash_profile becomes

alias bc='bc -l /PATH_TO/extensions.bc /PATH_TO/scientific_constants.bc'

After a while, I know I’ll forget where my bc extensions files are, so I include a message in my alias:

echo "Using bc -l with extensions in /PATH_TO/"

So my alias is

alias bc='echo "Using bc -l with extensions in /PATH_TO/";bc -l /PATH_TO/extensions.bc /PATH_TO/scientific_constants.bc'

Now every time I run bc, I see

Using bc -l with extensions in /PATH_TO/
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'.

And that way I can remember why it works just the way I want it to.

Cats: hack,unix