Use of 2>&1 in the shell

Source: Internet
Author: User
Tags file copy

A related knowledge 1) By default, the standard input is the keyboard, but it can also come from a file or pipe (pipe |). 2) By default, the standard output is terminal (terminal), but can also be redirected to a file, pipe, or post quotation mark (Backquotes '). 3) By default, the standard error output to the terminal, but can also be redirected to the file. 4) standard input, output and error output are expressed as stdin,stdout,stderr, or can be represented by 0,1,2. 5) In fact, in addition to the above commonly used in the 3 file descriptor, as well as 3~9 can also be used as a file descriptor. 3~9 you can think of as a file descriptor that executes somewhere, and is often used as a temporary intermediate descriptor.    two instance 1) command 2>errfile:command error redirected to file errfile. 2) Command 2>&1 | ...: Command error redirects to standard output, error and standard output are piped to the next command. 3) var= ' command 2>&1:command error redirect to standard output, error and standard output are assigned to Var. 4) command 3>&2 2>&1 1>&3 | ...: implements the interchange of standard output and error output. 5) var= ' command 3>&2 2>&1 1>&3 ': Implements the interchange of standard output and error output. 6) Command 2>&1 1>&2 | ...      (wrong ...): This does not enable the exchange of standard output and error output. Because the shell executes commands from left to right, when the 2>&1 is executed, the error output is the same as the standard output, and the execution of 1>&2 is meaningless.    three "2>&1 file" and "> File 2>&1" Difference 1) Cat food 2>&1 >file: Error output to terminal, standard output redirected to file. 2) Cat food >file 2>&1: The standard output is redirected to the file, and then the error output is redirected to the same as the standard output, so the error is also output to file files.    Four note that normally open files are automatically closed when the process is launched, but a better approach is when you're finished using them immediatelyShut down. Use m<&-to close the input file descriptor m and use m>&-to close the output file descriptor m. If you need to turn off the standard input <&-; The >&-is used to turn off standard output.   Five simultaneous output to terminal and file copy source Dest | Tee.exe copyerror.txt  Six references 1) http://docstore.mik.ua/orelly/unix/upt/ch45_21.htm2) http://www.unix.com/ SHELL-PROGRAMMING-SCRIPTING/34011-MEANING-DEV-NULL-2-1-A.HTML3) http://docstore.mik.ua/orelly/unix/upt/ch08_13. htm  Thanks, thanks!.  itech Source: http://itech.cnblogs.com/The copyright of the author Itech All, reproduced please include the author's signature and provenance, not for commercial purposes, non-prosecution legal responsibility!

Use of 2>&1 in the shell

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.