The meaning and difference of the shell 1>&2 2>&1 &>filename redirect _linux Shell

Source: Internet
Author: User
Tags stdin

In the shell, see ">&1" and ">&2" always do not understand what meaning. After the search on the internet to dispel doubts. Actually, this is two kinds of output.

In a shell program, the most commonly used FD (file descriptor) is about three, respectively:

0 is a file descriptor that represents the standard input (stdin)
1 is a file descriptor representing the standard output (stdout)

2 is a file descriptor that represents the standard error (STDERR)

In standard cases, these FD are associated with the following devices:
stdin (0): Keyboard keyboard input, and return in front
STDOUT (1): Monitor correct return value output to front end
STDERR (2): Monitor error return value output to front end

For example, let's say:

The current directory has only one file a.txt.
[Root@redhat box]# ls
A.txt
[Root@redhat box]# ls a.txt b.txt
Ls:b.txt:no such file or directory returned an error value because it did not b.txt the file, which is called the 2 output
A.txt and this is the so-called 1 output

And then look:

[Root@redhat box]# ls a.txt b.txt 1>file.out 2>file.err
After execution, there is no return value. The reason is that the return value is redirected to the appropriate file, not the front end display
[Root@redhat box]# Cat File.out
A.txt
[Root@redhat box]# Cat File.err
Ls:b.txt:no such file or directory
Generally speaking, "1>" can usually be omitted as ">".
That can be written as the above command: LS a.txt b.txt >file.out 2>file.err
With these knowledge, we can understand "1>&2" and "2>&1".
1>&2 the correct return value is passed to the 2 output channel &2 represents the 2 output channel
If this error is written in 1>2, the 1 output is redirected to file 2.
The 2>&1 error return value is passed to the 1 output channel, and the same &1 represents the 1 output channel.
Give an example.
[Root@redhat box]# ls a.txt b.txt 1>file.out 2>&1
[Root@redhat box]# Cat File.out
Ls:b.txt:no such file or directory
A.txt
Right now, both the correct output and the wrong output are directed to the File.out file, not to the front end.
Add, the output is not only 1 and 2, there are other types, these two are only the most common and most basic.

> is a redirection character that redirects the contents of the preceding output to a specified location at a later point, for example (Example 1):

echo "Some content" > filename.txt

The above example writes " some content " to the filename.txt file.

Before > , you can add a number to indicate what content is redirected to the file, by default, the standard output is redirected to the file, so the following example is the same as the one above (example 2):

echo "Some content" 1> filename.txt

If the error message is not output to filename.txt (example 3):

$ ls nodir 1> filename.txt
$ ls:nodir:No such file or directory

The above example Nodir does not exist, so the error message is output to 2 (stderr) when the LS command is queried, but we specify to redirect 1 to Filename.txt, so after the command is executed, there is no content in the filename.txt. But executing the following command will write the error message to Filename.txt (example 4):

$ ls nodir 2> filename.txt
$ cat filename.txt
$ ls:nodir:No such file or directory

& is a descriptor that, if not added before 1 or 2, will be treated as a normal file.

1>&2 means to put a standard output redirected to Standard Error .

2>&1 means redirecting the standard error output to the standard output.

&>filename means redirecting both standard output and standard error output to file filename in


Let's look at one more example (column 5):

$ ls nodir 1> filename.txt 2>&1
$ cat filename.txt
$ ls:nodir:No such file or directory

The example above redirects the standard output to the file filename.txtand redirects the standard error to the standard output , so the final error message is also written to the file through standard output, in comparison with example 3 , 4, 5, can understand its role.

The following is from Baidu to know the content, you can refer to the following:

Q: How do >&2 understand Linux redirection?
Question add: How does echo "AAAAAAAAAAAAAAAA" >&2 understand?

For:

>&2 that is to 1>&2 the result to the same as the standard error; If a standard error is redirected to a log file before, the standard output is redirected to the log file
such as: LS 2>a1 >&2 (equivalent to LS >a1 2>&1)
Both standard output and standard errors are redirected to A1, and no information is visible on the terminal.

Related Article

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.