Shell's >/dev/null, 2>&1, 2>1

Source: Internet
Author: User
Reprinted from: http://dos2unix.cn/link/480

1. Standard input stdin file descriptor is 0, standard output stdout file descriptor is 1, standard error stderr file descriptor is 2

2./dev/null empty equipment, equivalent to garbage bin

3. REDIRECT Symbol:>

3. The difference between 2>1 and 2>&1

    • 2>1, redirect standard error stderr to file 1
    • 2>&1, redirect standard error stderr to standard output stdout

4. For example:

Suppose there is a script test.sh, the content is as follows, T is a non-existent command, execute the script to test below.

Shell
# cat Test.sh T Date

The standard output is redirected to log, and the error message is output to the terminal, as follows:

Shell
#./test.sh > Log. /Test. SH: Line 1: t: command not Found # Cat LogThu Oct : £º --CST

Delete log file, re-execute, this time the standard output is directed to log, error message directed to the file 1

Shell 
#./test.sh > Log 2>1## Cat LogThu Oct : £º- CST # cat 1. /Test. SH: Line 1: t: command not Found #

REDIRECT standard output to log file, redirect standard error to standard output, i.e. redirect error message and output information to log

Shell
#./test.sh > Log 2>&1## Cat Log. /Test. SH: Line 1: t: command not found Thu Oct:: +- CST #

REDIRECT error messages to an empty device

Shell
#./test.sh 2>/dev/null Thu Oct :£ º CST #

REDIRECT standard output to an empty device

Shell

#./test.sh >/dev/null . /Test. SH: Line 1: t: command not Found #

Full redirect of standard output and standard error to empty device

Shell
#./test.sh >/dev/null 2>&1 #

Full redirect of standard output and standard error to empty device

Shell

#./test.sh >/dev/null 2>&1 #

Shell's >/dev/null, 2>&1, 2>1

Related Article
Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.