Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This does not make sense to me.

wibble > /dev/null 2>&1

I think it would make more sense if it was something like this:

wibble 2>&1 > /dev/null

In other words

Commands Output Sendall STDERRORS to STDOUT then SEND it all to /dev/null

What is the thinking behind the order of the command redirection xxx > /dev/null 2>1?

share|improve this question
5  
You might also find this explanation useful. –  rozcietrzewiacz Nov 4 '11 at 16:42
    
Thanks @rozcietzewiacz that actually explains my question exactly –  whoami Nov 4 '11 at 16:57
    
There’s an excellent passage in the middle of that page on Greg’s Wiki: “If you’re still confused at this point, it’s probably because you started out with a misconception about how FDs work, and you haven’t been able to drop that misconception yet.  Don’t worry – it’s an extremely common misconception, and you’re not alone.  …  Many people think that 2>&1 somehow “unites” or “ties together” or “marries” the two FDs, so that any [subsequent] change to one of them becomes a change to the other.  This is not the case.” –  G-Man Jul 26 at 19:23

1 Answer 1

up vote 35 down vote accepted

The redirects are processed from left to right. If you do

2>&1 1> /dev/null

The first redirect makes stderr point to the stream that stdout points to at that time (which is your tty essentially). It doesn't make stderr an alias of stdout.

Then stdout is redirected to the bit bucket. The stdout redirect doesn't affect the previous stderr redirect. stderr still refers to your tty.

So:

ls file_that_doesnt_exist 2>&1 1> /dev/null

will print only the error message on your terminal.

The bash redirection documentation page mentions this explicitly:

Note that the order of redirections is significant. For example, the command

          ls > dirlist 2>&1

directs both standard output and standard error to the file dirlist, while the command

          ls 2>&1 > dirlist

directs only the standard output to file dirlist, because the standard error was duplicated from the standard output before the standard output was redirected to dirlist.

share|improve this answer
3  
Yay solves my conundrum. I still think logically its confusing. but i found another part on the same page that is interesting &>/dev/null is semantically equivalent to /dev/null 2>&1 –  whoami Nov 4 '11 at 16:54
    
It should be emphasized that the page you are talking about is part of the Bash Reference Manual.  It, obviously, describes bash.  There are other shells in the world, different from bash, and &> and >& (when not used in the context 2>&1 or >&2, etc.) are bash-isms and are not standard.  They should not be used in shell scripts that you want to be portable. –  G-Man Jul 26 at 19:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.