2>&1 >/dev/null & and/dev/null 2>&1 meaning

Source: Internet
Author: User

/dev/null represents an empty device file

Where does the > delegate redirect to, for example: echo "123" >/home/123.txt

1 means stdout standard output, the system default is 1, so ">/dev/null" is equivalent to "1>/dev/null"

2 indicates stderr standard error

& means equivalent to, 2>&1, 2 output redirect equals 1

1>/dev/null first indicates that the standard output is redirected to an empty device file, that is, not outputting any information to the terminal, which is plainly not displaying 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.

Linux system, whether in the crontab inside, or usually use the command, often encounter ">/dev/null 2>&1". For example: In the crontab job, if you do not want to send mail, then there are two ways:

One, the mailto= "", set to Empty

[email protected] etc]# cat crontab
Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto= ""
home=/


# Run-parts
* * * * * Root run-parts/etc/cron.hourly
4 * * * Root run-parts/etc/cron.daily
4 * * 0 root run-parts/etc/cron.weekly
4 1 * * Root run-parts/etc/cron.monthly

Second, it will close the output of a command and redirect it to/dev/null.
1 1 * * */sbin/ping-c 5 www.linuxidc.com >/dev/null 2>&1
1 2 * * */sbin/ping-c 5 www.linuxidc.com >/dev/null

The first crontab, even if the ping fails, there will be no mail sent,
The second crontab, if the ping fails, generates a large number of error output prompt messages in the/var/spool/mail directory or/var/spool/clientmqueue directory.

So how to understand ">/dev/null 2>&1" can be disassembled to understand:

>: redirection, such as echo "Hello" >/root/1.txt
1> stdout: Indicates standard output, default is 1
2> stderr: Indicates standard error
&: Equals, 2>&1, indicates standard error redirection, equivalent to standard output

/dev/null: Represents an empty setup file, similar to a trash bin in a win system

Therefore, ">/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, the standard output is redirected to an empty device file, which means no information is displayed.
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.

For example:

    • Command > File 2>file

    • Command > File 2>&1

The same point: all two commands above are implemented to redirect the command, standard and error output to the file.

Different points: differences in I/O efficiency
Command 1>file 2>file Such a notation, stdout and stderr are sent directly to file, file will be opened two times, so stdout and stderr will cover each other, This writes quite a FD1 and FD2 two pipeline to preempt file at the same time.
Command 1>file 2>&1 This order will stdout directly 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 the I/O efficiency, the efficiency of the previous command is less efficient than the command in the next one, so when writing a shell script, we command 1>file 2>&1, or "command >file 2& 1 "



This article is from the "Dream to Reality" blog, please be sure to keep this source http://lookingdream.blog.51cto.com/5177800/1794991

2>&1 >/dev/null & and/dev/null 2>&1 meaning

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.