Sunday, June 22, 2008

> /dev/null 2>&1

Seen this? You will ask either of the following question:

1) What is it?
2) I know it is input/output redirection but how is it working?

Now the answer for both of you.

In *nix world STDIN, STDOUT, STDERR are three common things that you must know.
Standard Input i.e. STDIN or 0 is something like keyboard.
Standard Output i.e. STDOUT or 1 is something like Monitor
Standard Error i.e. STDERR or 2 is something like ERROR

You can manipulate them. For eg,
Instead of typing from keyboard you can input from other medium using input redirection.
Like print < /file

Instead of displaying to monitor you can output to other medium using output redirection. Like echo something > /otherfile
You can use >> to append

Instead of displaying error to standard error you can use error redirection.
Like cat somefile 2> /dev/null
cat somefile 2> /somefile
Here /dev/null is something like dustbin. If you don't want your error to be seen then simply redirect it to the dustbin.

Now let me answer the second question

> /dev/null 2>&1 redirect both output and error to /dev/null
> /file 2>&1 redirect both output and error to /file

> redirects output to /dev/null
2>&1 redirect error to where > is redirected
So error also gets redirected to /dev/null


  1. Thankyou very very much. I always wondered what the *** was this all aobut. Thankyou again. Very nice.