The difference between >/dev/null 2>&1 and 2>&1 >/dev/null and &>/dev/null in the shell

Source: Internet
Author: User

This article was reprinted from: 11595985

By default, there are always three files open, standard input (keyboard input), standard output (output to screen), standard error (also output to screen), and their respective file descriptors are 0,1,2. So let's take a look at the differences between the following redirection methods:

>/dev/null 2>&1

In fact, it should be equivalent to this: 1>/dev/null 2>/dev/null, by default is 1, standard output, so generally omitted. The & symbol, followed by the required file descriptor. cannot be written as 2>1, so it becomes a standard error redirect to a file named 1, instead of redirecting standard errors to standard output. So here it is: The standard output is redirected to the/dev/null, and the standard error is redirected to the standard output, so the standard output and standard error are redirected to the/dev/null

2>&1 >/dev/null

At first glance, what's the difference between this and that one? Standard error redirected to standard output, and standard output redirected to/dev/null? Shouldn't we all redirect/dev/null at the end? That's how I understand it! An instruction either produces a standard error at the same time or produces a standard output. When the output standard is wrong, the standard error is redirected to the standard output, and the standard output is output to the screen. At this point the standard output was not redirected to/dev/null, so it was printed on the screen. When the standard output is produced, then it is not a standard error, 2>&1 is not valid, so the standard output redirects dev/null, does not print to the screen. So the end result will be: standard error printing to the screen, and standard output not printing to the screen.


2016-01-22 Update

Suddenly see the blog n years ago, only to find that there are a great error. Update to avoid astray, in fact, command line redirection is ready before executing the command. The explanation sequence is left to right, 2&>1, and 1 is the screen, so the standard error redirects to the screen, and 1>/dev/null, the standard output redirects to/dev/null, the 2>&1 >/dev/null above is not The same time either produces standard output or produces a standard error. It's a two different thing.

To explain it in the following variable way, it is obvious that these two ways are different, the former is like:



And the latter is like:




This is, whatever you are. File descriptor, all redirected to/dev/null

The difference between >/dev/null 2>&1 in the shell and 2>&1 >/dev/null and &>/dev/null

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: 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.