*NIX Tricks

[bash] Command line calculator

Posted in bash, cli by kousik on October 6, 2009

Add the following function to the ~/.bashrc or ~/.bash_aliases, source the former and you’ll get a better command line calculator:

? () { echo "$*" | bc -l; }

You can use it the following way:

$ ? 7*3-5.5

and, no doubt that you’ll get 15.5 as the answer.

N.B. If you use parentheses, you must enclose the whole expression by double quotes.

 

UPDATE: An alternative way of using bc
$ bc -l <<< "7*3 - 5.5"

Tagged with: , , , ,

2 Responses

Subscribe to comments with RSS.

  1. [...] Posted in awk, cli, linux, osx, unix by kousik on November 3, 2009 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 [...]

  2. [...] 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 [...]


Leave a Reply

Fill in your details below or click an icon to log in:

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.