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.

9 comments:

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"

Camellia Canan said...

All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.


SAP training in Chennai

priya said...

It is really a great and useful piece of info. I’m glad that you shared this helpful info with us. Please keep us informed like this. Thank you for sharing.

Online Training in Chennai

Saki R said...

thanks for shared wonderful information of giving best information.its more useful and more helpful. great doing keep sharing
Java Training in Chennai

Philips Huges said...


Nice Sharing..! I have been following you for a couple of months now but this is my first time commenting on a blog post. Thank you for sharing your knowledge and experience with us. Keep up the good work. Already bookmarked for future reference.

Installment loans in Mississippi
Payday loans in Mississippi
Title loans in Mississippi

Philips Huges said...


Its very useful to me. Wonderful blog.. Thanks for sharing informative Post.

Installment loans
Payday loans
Title loans