I have for some year used a simple alias for the calculator command.
The alias only requires awk
(or gawk
), and
that tcsh
(or csh) is the running shell. This alias will
not work with bash/sh/ksh since these shells do not allow arguments in
aliases. Just place the following line in your ~/.tcshrc
or type at the prompt:
alias calc 'awk "BEGIN{ print \!* }" ' # or with fixed output format: alias calcfx 'gawk -v CONVFMT="%12.2f" -v OFMT="%.9g" "BEGIN{ print \!* }"' # When calling calc DO NOT escape "*": # Example: calc (3+5)*4/5
At a meeting in SSLUG (1997) I was given the idea to use
functions for making a bash equivalent of the above calc
alias.
In ~/.bash_profile:
function calc () { awk "BEGIN { print $* ; }" } function calcfx () { gawk -v CONVFMT="%12.2f" -v OFMT="%.9g" "BEGIN { print $* ; }" }
With bash it is nescesary to use quotes if the expression contains () or *. For example:
calc "(3+5) * 4/5"
Thanks to Dan Christiansen who sent the zsh version:
alias calc="noglob _calc" calcfx="noglob _calcfx" function _calc () { awk "BEGIN { print $* ; }" } function _calcfx () { gawk -v CONVFMT="%12.2f" -v OFMT="%.9g" "BEGIN { print $* ; }" }
Because of noglob
, it is not nescesary to use quotes
around the expression:
calc (3+5)*4/5
-- Frank Damgaard / email: frda(a)post3.tele.dk