Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Yet another simple command line Calculator UNIX
Add the following to your .bashrc for a quick calculator:
calc () { 
    echo "$@" | bc -l
}
Now at the prompt you may try something like...
$ calc "2 * 3F"
...and instantly receive the result -- no need for some GUI tool.

[robg adds: There are two other command line calculator hints here. This one worked for me, at least with the commands added to my .bash_profile file.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[21,900 views]  

Yet another simple command line Calculator | 4 comments | Create New Account
Click here to return to the 'Yet another simple command line Calculator' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Yet another simple command line Calculator
Authored by: nicksay on Aug 08, '06 04:42:25PM
I've used a simliar function for a while now:
calc() {
    [[ $1 == *%* ]] && s=0 || s=5
    echo "scale=$s; $1" | bc -l
}
This limits the decimal precision to 5 places normally (which I prefer instead of 20), but switches back to integer precision when using the mod operator (i.e. 10 % 3).

The only requisite is to put your calculation in quotes. (i.e. calc '5 / 2').

[ Reply to This | # ]
Yet another simple command line Calculator
Authored by: cbiagini on Aug 08, '06 05:19:49PM
On a related and shamelessly self-promoting note, if you're the sort that likes command-line calculators and has an always-on Internet connection, I wrote a command-line interface to the Google Calculator that I like to think is pretty useful.

[ Reply to This | # ]
Yet another simple command line Calculator
Authored by: Anonymous on Aug 09, '06 09:52:52AM
Of course, if you're using bash anyway, you can just go ahead and use

$((expression))

and it even takes variables directly, allowing things like incrementing, both pre

$((++varname))

and post

$((varname++))



[ Reply to This | # ]
Yet another simple command line Calculator
Authored by: harryrh on Aug 18, '06 07:58:25AM

man expr



[ Reply to This | # ]