Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Save settings for bc, the Linux calculator

tl;dr

Use a ~/.bcrc file with contents:

scale=4

Execute this or put it in your bashrc:

export BC_ENV_ARGS=~/.bcrc

Explanation

In GNU/Linux, a basic calculator is bc from package bc. The normal output is displayed with zero values after the decimal point, so I like to adjust it to show me four numerals past the decimal point.

scale=4

You can use whatever scale you like. To get this to take effect every time you call bc, you can set an environment variable BC_ENV_ARGS to a filename. That file is parsed as regular instructions to bc, before it enters the interactive shell or interprets standard in. So modify a file, such as ~/.bcrc with your instructions. Update your shell (or add to your .bashrc file):

test -f ~/.bcrc && export BC_ENV_ARGS=~/.bcrc

References

Weblinks

  1. Inspiration for this post http://linux.byexamples.com/archives/42/command-line-calculator-bc/
  2. Description of "rc" files in Unix https://en.wikipedia.org/wiki/Run_commands

man pages

bc

Comments