Why can't we just close stdout for a process?

I was trying out redirection commands. The following works:

ls -a 0>&-
ls -a 2>&-

which means close stdin and stderr for the process ls -a. But why does closing stdout fail? I am getting

aniket@aniket-Compaq-610:~/Downloads$ ls -a 1>&-
ls: write error: Bad file descriptor

I know it does not make sense to close stdout here but I am wondering why it is not allowed?

1 Answer 1


Obviously closing stdout does not fail, on the contrary, it succeeds because writing on it fails, as can be seen from the error message. Edit: to clarify my answer, what happens is that you first tell the shell to close the file descriptor, then the ls program tries to write to it. This is where the error message comes from.

  • That makes sense. I did not look at it that way. Thanks! Oct 15, 2016 at 19:05
  • 1
    As a general rule, this is why the file-descriptor closing, although in some ways neater than redirection to /dev/null, can often be a problem, especially for programs that expect to be able to write output. Some programs just don't read/write on specific file descriptors, but more careful programs will report reading/writing errors on stderr, like any other error, and will exit with an error status accordingly, even if otherwise successful.
    – mtraceur
    Oct 16, 2016 at 10:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .