Shell standard output error >/dev/null 2>&1 analysis "Go" in Linux

Source: Internet
Author: User

The shell can often be seen: >/dev/null 2>&1

Eg:sudo kill-9 ' ps-elf |grep-v grep|grep $1|awk ' {print $4} ' 1>/dev/null 2>/dev/null

The result of the command can be defined in the form of a%> output

/dev/null represents an empty device file

Where does the > delegate redirect to, for example: echo "123" >/home/123.txt
1 means stdout standard output, the system default is 1, so ">/dev/null" is equivalent to "1>/dev/null"
2 indicates stderr standard error
& means equivalent to, 2>&1, 2 output redirect equals 1

Then the statement in the title of this article:

1>/dev/null first indicates that the standard output is redirected to an empty device file, that is, not outputting any information to the terminal, which is plainly not displaying any information.

2>&1 then, the standard error output redirection is equivalent to the standard output because the standard error output is redirected to the empty device file because the standard output was previously redirected to an empty device file.

2>&1 written in the back of the reason

Format: command > file 2>&1 = = Command 1> file 2>&1

First, command > file redirects the standard output to file, and 2>&1 is the standard error copy of standard output, which is also redirected to file, and the end result is that standard output and errors are redirected to file.

If you change to: command 2>&1 >file

The 2>&1 standard error copies the behavior of the standard output, but at this point the standard output is output to the terminal. When >file, the standard output is redirected to file, but the standard error still retains the previous setting, that is, to keep the output to the terminal.

Scripts use only standard input, standard output, and standard errors

The shell will automatically open and close the three file descriptors for us 0, 1, 2, and we don't need to open or close them explicitly. The standard input is the input of the command, the default point to the keyboard, the standard output is the output of the command, the default point to the screen, the standard error is the output of the command error message, the default point to the screen.

If you do not explicitly redirect, the command reads the input from the screen through the file descriptor 0, outputting the output and error information to the screen through file descriptors 1 and 2. But if we want to read input or produce output from other files (again, the I/O device is also a file in unix/linux), we need to redirect the 0, 1, and 2 uses. Its syntax is as follows:


Command < filename redirects the standard input to the filename file
Command 0< filename redirects the standard input to the filename file

Command > FileName redirect the standard output to the filename file (overwrite)
Command 1> fielname redirect the standard output to the filename file (overwrite)

Command >> filename redirects the standard output to the filename file (append)
Command 1>> filename redirects the standard output to the filename file (append)

Command 2> filename redirects the standard error to the filename file (overwrite)
Command 2>> filename redirects the standard output to the filename file (append)

Command > FileName 2>&1 redirect standard output and standard error to filename file (overwrite)
Command >> filename 2>&1 redirect standard output and standard error to filename file (append)

Command < filename >filename2 REDIRECT standard input to the filename file and redirect the standard output

To the Filename2 file
Command 0< filename 1> filename2 redirect The standard input to the filename file, redirecting the standard output

To the Filename2 file

The use of redirection has the following rules:

1) standard input 0, Output 1, error 2 need to be redirected separately, one redirect can only change one of them.
2) standard input 0 and standard output 1 can be omitted. (when it appears to the left of the redirect symbol)
3) The file descriptor is written directly to the left of the redirect symbol, and & is preceded by the right side.
4) There can be no space between the file descriptor and the redirect symbol!

Shell standard output error >/dev/null 2>&1 analysis "Go" in Linux

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.