Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set a default precision for the Unix dc calculator? Is there a config file for it?

Tags:

unix

dc

You can set the precision after invoking dc with the 'k' command, which pops a number off the stack and uses it to set the precision. But I always want a precision of three digits after the decimal by default. Is there a way to set a default precision in dc?

like image 250
Magneto Avatar asked Sep 25 '22 05:09

Magneto


1 Answers

What I've done to achieve this is create a file in your home directory called .dcinit with the commands you want executed every time, e.g.

bash-3.2$ cat .dcinit
5 k
bash-3.2$ 

Then define an alias in your startup config file for dc that loads the startup file and then reads from stdin:

bash-3.2$ alias dc="dc -f ~/.dcinit -"

You should be able to use it as normal interactively but it will first load your .dcinit:

bash-3.2$ dc
4 5 / p 
.80000

If you need to run it on a file of dc commands, you'll have to disable the alias or run it explicitly from /usr/bin/dc or whereever.

like image 126
cdlane Avatar answered Sep 28 '22 04:09

cdlane