Common standard input, standard output, and standard error redirection in shell scripts

Source: Internet
Author: User

1, you can consider/dev/null as a "black hole." It is very equivalent to a write-only file. All the content written to it is lost forever. and trying to read from it is nothing. However,/dev/null is very useful for command lines and scripts.  
Useful:  
prohibit standard output .    1 cat $filename >/dev/null   # file Contents missing, Not output to standard output.  
suppresses standard errors.    2>/dev/null This error message [standard error] was thrown into the Pacific Ocean.  

2, 1>/dev/ The meaning of the null 2>&1  

> represents where to redirect, for example: echo "123" >/home/123.txt 
1 for stdout standard output, The system default value is 1, so ">/dev/null" is equivalent to "1>/dev/null"  
2 means stderr standard error  
& means equivalent,2>& 1, which indicates that the output redirection of 2 is equivalent to 1 

the statement in the title of this article:  
1>/dev/null first indicates that standard output is redirected to an empty device file. That is, do not output any information to the terminal, it is plainly not to display any information.  
2>&1 then, the standard error output redirection is equivalent to 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.  

3,/dev/zero file represents a permanent output 0 of the device file, use it as input to get all empty files. You can therefore use it to create new files and to erase old files in a way that overrides them.  

Use the DD command to create a 10K size from a zero device (BS decides to read and write 1024 bytes each, and count defines read and write times 10), but the contents are all 0 files. &NBSP
DD If=/dev/zero of=file count=10 bs=1024

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


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

Break down this combination: ">/dev/null 2>&1" is part five.

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

so,> /dev/null 2> &1 can also be written as "1>/dev/null 2> &1"

Then the statement execution procedure for this article title is:
1>/dev/null: First of all, the standard output redirects to the empty device file, that is, do not output any information to the terminal, it is plainly not to display any information.
2>&1: The standard error output is then redirected to 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.

The most commonly used methods are:

Command > File 2>file with command > file 2>&1

Is there anything different about them?
The first command > file 2>file means to send the standard output information generated by the commands to file with the wrong output information. Command > File 2>file StdOut and stderr are sent directly to file, file will be opened two times, so stdout and stderr will cover each other, so write quite use FD1 and FD2 two simultaneously to seize the file of the pipeline.
and command >file 2>&1 This order will stdout directly sent to file, stderr inherit the FD1 pipeline, and then sent to file, at this time, file was opened only once, also only used a pipeline FD1, It includes the contents of stdout and stderr.
From IO efficiency, the efficiency of the previous command is less efficient than the one in the following command, so when writing a shell script, we will command > file 2>&1.

Common standard input, standard output, and standard error redirection in shell scripts

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.