Shell: redirection of Input and Output

Source: Internet
Author: User

Understanding:

By default, Linux commands obtain input from the standard input device (stdin) and output the result to the standard output device (stdout) for display. Generally, the standard input device is the keyboard, and the standard output device is the terminal, that is, the display. When running commands in Linux Shell, each process is associated with three open files and uses file descriptors to reference these files. Because the file descriptor is not easy to remember, shell also provides the corresponding file name:

File descriptor description list

File File descriptor
Input File-standard input 0 (Keyboard by default; 0 is the output of files or other commands)
Output file-standard output 1 (screen by default; 1 is a file)
Error output file-standard error 2 (screen by default; 2 is a file)


List of all available command lines

Command Description
Command> File Redirects the output to file.
Command <File Redirects the input to file.
Command> File Redirects the output to file in append mode.
N> File Redirects a file whose file descriptor is n to a file.
N> File Redirects an object whose file descriptor is n to a file in append mode.
N> & M Merge the output files M and N.
N <& M Merge the input files M and N.
<Tag The content between the start tag and end tag is used as the input.

By default, command> file redirects stdout to file, and command <file redirects stdin to file.


  • If you want stderr to redirect to file, you can write:

$ Command 2> file #2 indicates a standard error file (stderr)

  • If you want stderr to append to the end of the file, you can write:

$ Command 2> file #>> indicates appending and writing, without overwriting the previous content.

  • If you want to merge stdout and stderr and redirect to file, you can write:

$ command > file 2>&1

Or

$ command >> file 2>&1

  • If you want to redirect both stdin and stdout, you can write:
$ Command <infile> OUTFILE # command redirects stdin to infile and stdout to OUTFILE


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.