Shell standard output, standard error

Source: Internet
Author: User

The shell can often see: >/dev/null 2>&1 eg:sudo kill-9 ps-elf |grep-v grep|grep $1|awk ' {print $4} ' 1>/dev/null 2> The result of the/dev/null command can be defined in the form of%> output

/dev/null represents the empty device file ">" for redirection, for example: echo "123" >/home/123.txt 1 represents stdout standard output, the system default is 1, so ">/dev/null" equals "1>/" Dev/null "2 means stderr standard error & denotes equivalent meaning, 2>&1, indicating 2 output redirection 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, does not output any information to the terminal, plainly, it does not display 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.

/////////////////////

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 REDIRECT the standard output to the filename file (append) command 1>> filename redirects standard output to the filename file (append) command 2> filename REDIRECT standard error to 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 redirects the standard input to the filename file, redirecting 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, standard error

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.