The meaning and difference of the shell 1>&2 2>&1 &>filename Redirection

Source: Internet
Author: User
Tags stdin



In the 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 that represents the standard output (stdout)



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



In the standard case, these FD are associated with the following devices, respectively:
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






Give me a description:



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






And then look at:


[[email protected] 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
[email protected] box]# cat File.out
A.txt
[email protected] box]# cat File.err
Ls:b.txt:no such file or directory
In general, "1>" can usually be omitted as ">".
That can be written as above command: LS a.txt b.txt >file.out 2>file.err
With these understandings, "1>&2" and "2>&1" can be understood.
1>&2 the correct return value is passed to the 2 output channel &2 represents 2 output channel
If 1>2 is incorrectly written here, it means redirecting the 1 output 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 me a chestnut:
[[email protected] box]# ls a.txt b.txt 1>file.out 2>&1
[email protected] box]# cat File.out
Ls:b.txt:no such file or directory
A.txt




The correct output and incorrect output are now directed to the File.out file, not to the front end.
In addition, the output is not only 1 and 2, there are other types, both of which are just the most common and basic.



> is the redirect, which redirects the contents of the preceding output to the location specified later, such as (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 to redirect to the file, the default is to redirect the standard output to the file, so the following example is the same as the above (example 2):


echo"Some content"

1> filename.txt


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


1

2


$ lsnodir 1> filename.txt

$ ls: nodir: No such fileor directory






In the above example, Nodir does not exist, so the LS command to query the error message will be output to 2 (stderr), but we specify to redirect 1 to Filename.txt, so the above command after execution, filename.txt is not content. However, executing the following command will write the error message to Filename.txt (example 4):




$ lsnodir

 2> filename.txt



$  CatFilename.txt


$ ls: nodir: No such fileor directory



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



1>&2 means to put the standard output Redirect to Standard Error .



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



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






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




$ lsnodir 1> filename.txt 2>&1



$ catfilename.txt



$  LS: Nodir:no such fileor directory



The above example redirects the standard output to the file filename.txtand then redirects the standard error to the standard output , so the final error message is also written to the file via standard output, comparing example 3, 4,5, you can understand its role.



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



Q: How does >&2 understand Linux redirection?
Question supplement: echo "Aaaaaaaaaaaaaaaa" >&2 how to understand?



For:



>&2 that is 1>&2 the output to the same standard error, before the standard output is redirected to a log file if a standard error is defined
such as: LS 2>a1 >&2 (equivalent to LS >a1 2>&1)
REDIRECT standard output and standard error to A1, no information is visible on the terminal.



The meaning and difference of the shell 1>&2 2>&1 &>filename Redirection


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.