What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.


Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:



2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.




Define a quick calculator function

Terminal - Define a quick calculator function
? () { echo "$*" | bc -l; }
2009-06-28 20:15:30
User: fizz
Functions: bc echo
Define a quick calculator function

defines a handy function for quick calculations from cli.

once defined:

? 10*2+3


There is 1 alternative - vote for the best!

Terminal - Alternatives
=() { echo $(($*)); }
2013-05-03 04:27:07
User: xlz
Functions: echo

POSIX compliant arithmetic evaluation.

= 10*2+3

alias ?=concalc
2014-01-02 01:46:44
User: boynux
Functions: alias

Same functionality without using bash functions.

Know a better way?

If you can do better, submit your command here.

What others think

Another way to do that would be:

? () { let myresult="$*" ; echo $myresult; }

That would avoid changing context (launching a new process, that's what "bc" is) and would be much faster if you are using the calculations in some loop or whatever.

To ensure that, I made a quick test: I defined "?" as my function, and "x" as your function, and measured their times for a simple compound calculation:

time ( for a in {0..999}; do ? "($a*$a-$a)/($a+$a+1)" > /dev/null ; done )

real 0m0.767s

user 0m0.624s

sys 0m0.144s

time ( for a in {0..999}; do x "($a*$a-$a)/($a+$a+1)" > /dev/null ; done )

real 0m6.777s

user 0m1.544s

sys 0m4.548s

Of course, it might be that you would miss some functionality for advanced calculations using the internal shell syntax instead of bc. But then again, bc (-l or not) has its limitations: you can't use it for non-integer exponentials, that is:

echo "2^(1/3)" | bc -l

Runtime warning (func=(main), adr=9): non-zero scale in exponent

I would recommend installing the "concalc" package if you need better mathematical evaluations in shell commands:

echo "2^(1/3)" | concalc


Comment by Patola 295 weeks and 6 days ago

sweet! many uses for this, also thanks for concalc...

Comment by wires 295 weeks and 5 days ago

I prefer the Pari calculator which in this situation can be invoked as "gp -q". It gives you tons of functions and an obscene number of decimal points.

For some reason when I try to run the "?" function defined above I just get "bash: z: command not found" and for "help ?" I get "help: no help topics match `z'". Any ideas why?

Comment by dstahlke 295 weeks and 5 days ago

@dstahlke this is because you have a file named z in the current directory, the shell expands ? to z, so you get this error message

Comment by unixmonkey4488 295 weeks and 5 days ago

As long as I've been bash'ing, I didn't know you could use


as a function name. Thanks for the example for that, even if I don't use the calculator function! :)

Comment by hal0thane 295 weeks and 4 days ago

Is it actually necessary to define a function for "bc"?

It is simple enough to do this:

<<< "x=1; y=2; p=3*x+2*y; p" bc -l
Comment by mpb 295 weeks and 4 days ago

you can use bash format for very simple calculations like

echo $[ 1*2 + 3 ]


Comment by elementa13 295 weeks and 4 days ago
bc <<< 10*2+3
Comment by mpb 295 weeks and 1 day ago

Here is what I have defines: ? for bash arithmetic, calc for awk arithmetic, and a here-string for bc -l arithmetic:

# bash arithmetic function ? { echo $[$*] } # awk arithmetic (floating point result) function calc(){ awk "BEGIN{ print $* }" ;} bc -l<<<3/7
Comment by greppo 266 weeks and 4 days ago

Different calculators for different levels:

bash for simplest integer operations:

echo $[2*(3+5)]


awk for floating point and powers

function ? { awk "BEGIN{ print $* }" ;} ? '((2+1)/7)^3.5'


concalc for full scientific functions:



bc for high precision:

bc -l<<<3/7


gp for number theory:

gp -qf

2.6040699049291378729513930560926568818 E574964

Comment by greppo 266 weeks and 4 days ago

Fixing that:

gp for number theory:

gp -qf

2.6040699049291378729513930560926568818 E574964

Comment by greppo 266 weeks and 4 days ago

gp for number theory:

gp -qf<<<'123456! + 0.'

2.6040699049291378729513930560926568818 E574964

Comment by greppo 266 weeks and 4 days ago

Now if anyone has a way to delete or modify typos in comments, please let me know.

Comment by greppo 266 weeks and 4 days ago

I won't be using this exact command because I don't need a specific function setup for this, but I didn't know about the bc command even though I've been using linux for many years. It's pretty nice. Thumbs up.

Comment by sonic 158 weeks ago

Your point of view

You must be signed in to comment.

Related sites and podcasts