Linux SCRIPT>/dev/null 2> & 1, and 2> 1 vs 2> & 1

Source: Internet
Author: User

1. The standard input stdin file descriptor is 0, the standard output stdout file descriptor is 1, and the standard error stderr file descriptor is 2

2./dev/null empty device, equivalent to a waste bin

3. Redirection symbols:>

3. 2> 1 and 2> & 1
2> 1. Redirect stderr to file 1.
2> & 1. Redirect stderr to stdout

4. Example:
Suppose there is a script test. Sh with the following content. T is a non-existent command. Execute the script to perform the following test.
# Cat test. Sh
T
Date

The standard output is redirected to log, and the error information is output to the terminal as follows:
#./Test. Sh> log
./Test. sh: Line 1: T: Command not found
# Cat log
Thu Oct 23 22:53:02 CST 2008

Delete the log file and execute it again. This time, the standard output is directed to log, and the error message is directed to file 1.
#./Test. Sh> log 2> 1
#
# Cat log
Thu Oct 23 22:56:20 CST 2008
# Cat 1
./Test. sh: Line 1: T: Command not found
#

Redirects the standard output to the log file and redirects the standard error to the standard output file.
#./Test. Sh> log 2> & 1
#
# Cat log
./Test. sh: Line 1: T: Command not found
Thu Oct 23 22:58:54 CST 2008
#

Redirect error messages to empty Devices
#./Test. Sh 2>/dev/null
Thu Oct 23 23:01:07 CST 2008
#

Redirects the standard output to an empty device.
#./Test. Sh>/dev/null
./Test. sh: Line 1: T: Command not found

Redirect all standard output and errors to empty Devices
#./Test. Sh>/dev/null 2> & 1
#

 

 


Redirect all standard output and errors to empty Devices
#./Test. Sh>/dev/null 2> & 1

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.