Make2 & gt; & amp; 1

Source: Internet
Author: User
Backgroundofthispost: Recently, random, stdout, andstderr, especially, theyalwaysaskthesamequestionas: whatdoes21exactlymean? So, hereIwanttogiveasummar

Background of this post: Recently, I always noticed that Should guys can't distinguish the meaning among stdin, stdout, and stderr, especially, they always ask the same question: what does 21 exactly mean? So, here I want to give a summar

Background of this post:

Recently, I always noticed that Should guys can't distinguish the meaning among stdin, stdout, and stderr, especially, they always ask the same question: what does "2> & 1" exactly mean?

So, here I want to give a summary of this three things, and of course, I 'd like to take this "2> & 1" as an example.

-----------------------
Interpretation:

First of all, I want to give out the exact meaning of "2> & 1 ":

2> & 1:

0 is stdin. 1 is stdout. 2 is stderr
-------------
There's one way to remember this construct (maybe it is not entirely accurate ):
-------------
First of all, 2> 1 looks like a very good way to redirect stderr to stdout. but remember, it hasA very harmful disadvantage:It may be actually be interpreted"Redirect stderr to a file named 1".
-------------
&Indicates that what followsIs a file descriptorAnd not a filename. So the construct becomes: 2> & 1.
-------------
> &Is shell syntax"Fold a file descriptor into another", You can also interprete it as" it is the syntax to redirect a stream to another file descriptor"

So, now, have you got the meaning of what does "2> & 1" exact mean? I think you do!

Yes, it's very easy to understand, but what's more, I want to go little deep into this topic.

-----------------------
Furthermore:

Following picture gives a very clear defination of what is stdin stdout and stderr:

# In Linux everything is a file.
# Your hardware is also a file:
* 0-Input-Keyboard (stdin)
* 1-Output-Screen (stdout)
* 2-Error-Screen (stderr)

# For standard output:

Clear enough?

If you still feel confused about this, you can view this page to find more infomation:
Http://tldp.org/LDP/abs/html/io-redirection.html

-----------------------
Example:

Finally, I want to give you a example to end this entry (Try to understand it then _^ ):

The output of a. out is the following, without the 'stdxxx: 'prefix.

STDERR: stderr output
STDOUT: more regular

./a.out 3>&1 1>&2 2>&3 3>&- | sed 's/e/E/g'
more regular
stderr output

  1. First save stdout as & 3 (& 1 is duped into 3 ).
  2. Next send stdout to stderr (& 2 is duped into 1 ).
  3. Send stderr to & 3 (stdout) (& 3 is duped into 2 ).
  4. Close & 3 (&-is duped into 3)

-----------------------
Others:

If you want to know what'sThe difference between "cmd> file 2> & 1" and "cmd> file 2> file", Pls refer this post which written by r2007:
Http://bbs.chinaunix.net/thread-764727-1-1.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.