Shell I/O redirection in Linux system

Source: Internet
Author: User
Tags stdin linux

I/O redirect Detailed and application example

1, the basic concept (this is to understand the knowledge behind the premise, please be sure to understand)

A, I/O redirection is usually related to FD, Shell FD is usually 10, namely 0~9;

b, commonly used FD has 3, for 0 (stdin, standard input), 1 (stdout, standard output), 2 (stderr, standard error Output), the default and keyboard, monitor, monitor related;

C, the use of < to change the read Data channel (stdin), from the designated file read;

D, use > To change the sent out data channel (stdout, stderr), so that the output to the designated file;

E, 0 is the default value of <, so < is the same as 0<;,> is the same as 1>;

F, in IO Redirect, stdout and stderr pipeline will be ready before the data from stdin read;

G, Pipeline "|" (Pipe line): The stdout of the previous command receives the stdin of the next command;

H, tee command is without affecting the original I/O, the stdout copies of a copy to the file;

I, Bash (Ksh) procedure to execute a command: Parse command-variable Evaluation-command substitution (' and $ ())-redirect-wildcard expansion-Determine path-execute command;

J, () put command group Sub-shell to execute, also known as nested Sub-shell, it has a very important feature is: Inherit the parent shell standard input, output, and error plus any Other open file descriptors.

K, EXEC command: Often used to replace the current shell and restart a shell, in other words, did not start the child shell. Any existing environment will be purged when this command is used. When exec operates on a file descriptor, it is only then that EXEC does not overwrite your current shell environment.

2. Basic IO

cmd > file redirects the stdout to the file;

cmd >> file redirect stdout to file (append);

CMD 1> Fiel to redirect stdout to file;

cmd > File 2>&1 redirects stdout and stderr to file files;

cmd 2> file to redirect stderr to file;

cmd 2>> file to redirect stderr to file (append);

cmd >> file 2>&1 redirects stderr and stderr to file files (append);

cmd < file >file2 cmd command with the file file as stdin, to file2 files as stdout;

Cat <>file opens file in read-write mode;

cmd < file cmd command to use file as stdin;

CMD << delimiter here document, read from stdin, until it encounters the delimiter delimiter.

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.