Why would someone do this

(exec <&- >&- 2>&-; gzip somefile.txt) 2>&1 &

<&- >&- 2>&- "...means close stdin, stdout and stderr, respectively.."

but why do "2>&1" if it's closed?


The statement mentioned is incorrect because the standard file descriptors must be open.

POSIX specification: C.2.7 Redirection

Applications should not use the [n]<&- or [n]>&- operators to execute a utility or application with file descriptor 0 not open for reading or with file descriptor 1 or 2 not open for writing, as this might cause the executed program (or shell built-in) to misbehave. In order not to pass on these file descriptors to an executed utility or application, applications should not just close them but should reopen them on, for example, /dev/null. Some implementations may reopen them automatically, but applications should not rely on this being done.

The instruction would rather be

gzip somefile.txt >/dev/null 2>&1 &

There's no reason to do what they did. The 2>&1 is indeed completely redundant in that example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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