Thursday, July 23, 2009

Redirection and 2>&1

In Unix, one often finds a input and output redirection "<", ">", ">>". These are simple to understand.

Users can also do redirect one stream into another stream. This is done by using standard file descriptors. So, if someone wants to redirect all standard errors to whereever standard output is going, add 2>&1 in the end of the command. 2>&1 means send the standard errors to where standard output is going. 2 is the default file descriptor for stderr and 1 for stdout. If certain command is redirecting stdout to, say some file, stderr still goes to default screen. If you want stderr also to be sent to the file where stdout is going, use 2>&1.
Similarly, you can also redirect stdout to whereever stderr is going by 1>&2.

Also, remember that "some unix command > some_file 2>&1" is different from "some unix command 2>&1 > some_file". The first one redirects stderr to some_file, whereas the second won't.

6 comments:

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Biz said...

Hi,
This is great work and very good information. This post will really help beginners, although it is basic but, it will help others in great deal in future.


Business Plan Service

Gamepoly.NET said...
This comment has been removed by a blog administrator.
Pedro Garcia Millan said...

ESTAFA AUTOMOTRIZ MEXICO!

http://pedromillan.blogspot.com/

PELIGROSISIMO AMIGOS MEXICANOS!

LINUX RULZ!!!!!!!!!!!!!!!!!

siva said...

hi,thank you,very nice blog.I had also writtened one blog."http://www.howtoearnwithinternet.blogspot.com"