At times when there's no good calculator within the reach of your hand and you are in dying need to multiply two numbers or do some other simply math operation (2*2?), I suggest you use perl one-liner to do the job. Just include this line in your .tcshrc or .cshrc (or any other such file):
alias calc perl -e \'print \"Simple Calculator\\n\\nResult: \" . \(\!\ +*\) .\"\\n\\n\"\;\'
(*note: you might have to prepend a full path to the 'perl' command.. something like '/usr/local/bin/perl' instead of simply 'perl')

To use it, just type 'calc (some mathematical expression)' in your shell prompt.
Example:
    > calc 5*5
   
Will print this:
Simple Calculator

Result: 25
Of course, you could use 'calc' to compute any other valid math expression. ;-)

"There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith

Replies are listed 'Best First'.
Re: Simple command line calculator
by jmcnamara (Monsignor) on Mar 15, 2002 at 09:00 UTC

    On Unix there is also the bc(1) utility which performs arbitrary precision arithmetic.
    $ bc 1 + 3 4 2^4 16 123456789012345678901234567890 * 2 246913578024691357802469135780 ibase=16 FF 255 quit

    Remember kids, Unix isn't just there to start perl, it has a few other uses as well. ;-)

    --
    John.

      It doesn't do floating point, though, which is why I often do use perl in non-debugging interactive mode or use a one-liner.
      perl -e 'print 5/2, "\n"';
      I like getting `2.5' there instead of just `2'.

      Or, just do this:
      # perl
      
      which will let you type a program into STDIN for perl to execute. Perfect if you need to make three or four calculations.

        It doesn't do floating point, though

        It does do floating point. Although, by default bc shows the results of calculations as integers. However, that can be changed either by setting scale or by invoking it with the -l option:

        $ bc 5/2 2 scale=4 5/2 2.5000 $ bc -l 5/2 2.50000000000000000000

        --
        John.

Re: Simple command line calculator
by premchai21 (Curate) on Mar 14, 2002 at 19:47 UTC

    Or you could just use Python or Ruby in interactive mode...

        perl -ple'$_=eval'
        is a bit easier to use :)

        U28geW91IGNhbiBhbGwgcm90MTMgY
        W5kIHBhY2soKS4gQnV0IGRvIHlvdS
        ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
        geW91IHNlZSBpdD8gIC0tIEp1ZXJk
        

        The Perl debugger interface, to me, is too cluttered for this purpose, especially considering the -- what are they, ANSI control codes? -- formatting. Python's interactive mode is somewhat better, but Python doesn't do math quite as well as Perl, I think. Interactive Ruby (irb), out of the three, is IMHO the best; as two examples, Ruby automatically switches to bignums when necessary, and is not as whitespace-dependent as Python (important in interactive mode)... the major disadvantage is that it is much less common than Perl and Python, or so I've seen.