What "2>&1" means in a Linux shell

Source: Internet
Author: User

You can often see them in a scheduled task. Examples of our company's planning tasks include:

*/2 * * * * * root cd/opt/xxxx/test_s1/html/xxxx/admin; PHP index.php task Testone >/dev/null2>&1*/2 * * * * Root cd/opt/xxxx/test _s1/html/xxxx/admin; PHP index.php task testtwo >/dev/null2>&1

For & 1 It is more accurate to say file descriptor 1, while 1 identifies standard output, stdout.
For 2, indicates a standard error, stderr.
2>&1 means redirecting standard errors to standard output. Here the standard output has been redirected to/dev/null. Then the standard error will be output to/dev/null

You can think of/dev/null as a "black hole". It is equivalent to a write-only file. All content written to it will be lost forever. While trying to read from it, nothing can be read.

Occasionally you can add & at the end of the command, indicating that the program is executed in the background.

Why 2>&1 to write in the back?

index.php task Testone >/dev/null2>&1

We can understand that the left side is the standard output, OK, now the standard output is directly input into/dev/null, and 2>&1 is to redirect the standard error to the standard output, so when the program generates an error, the error is equivalent to the left side, The left side is still entered into the/dev/null.

Can be understood as, if written in the middle, that will be the partition standard output of the specified output file

You can use

LS 2>1 test, will not report the error of No 2 file, but will output an empty file 1;
ls xxx 2>1 test, no xxx This file error output to 1;
ls xxx 2>&1 test, will not generate 1 of this file, but the error ran to the standard output;
ls xxx >out.txt 2>&1, actually can be replaced with ls xxx 1>out.txt 2>&1; redirect symbol > default is 1, error and output are uploaded to OUT.txt.

What "2>&1" means in the Linux shell

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.