Why do 2>&1 (redirect stderr to stdout) with <&- >&- 2>&- (close stdin, stdout, stderr)

io-redirection

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?

Best Answer

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 &
Related Question