Ramblings

November 24, 2009

Commandline calculator for OSX

Filed under: osx, tool — michaelangela @ 2:39 pm

Every now and then I like to do a bunch of quick calcs with references back to previous calcs. I can use iPython which is really powerful tool since it’s an advanced interactive frontend to python. But this is just a neat little tip.

[awk] Command line calculator using awk « *NIX tricks

This is an update on our bash command line calculator posted a few days ago — except for the fact that this time we’ll use awk to do the calculation instead of bc. As I mentioned in that post, you may use python or ruby (irb) to do the same thing, but these tricks may be useful if you don’t ruby or python installed (bc and awk, in general, come by default in any Unix or GNU/Linux distro).

First, create (or rewrite if you used our last trick) function “?” as follows and put it in your ~/.bashrc file:
? () { awk “BEGIN{ print $* }” ;}

and make sure to reload your ~/.bashrc file (do the similar thing if you’re using any other shell). [NOTE: ZSH does not like “?” as a function, so you might consider replacing it with something reasonable, e.g., “compute”]

Now, if you want to calculate an expression, do it, for example, as
$ ? “2*3+4.0*(9.9+8.1)”

and don’t forget the quotes.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: