Could an existing fd always be duplicated both as input and output? Could an existing fd always be duplicated both as input and output? shell shell

Could an existing fd always be duplicated both as input and output?


Never.Under the hood, the redirections will attempt to dup an existing or free filedescriptor and then the shell should proceed to run true if the preliminary redirections succeeded.

duping from an unopen descriptor should always fail, which effectively means one attempted redirection in either direction should be enough to test whether a filedescriptor is open or not.

( Conversely,duping from an open descriptor should almost always succeed. It fails when you've run out of your per-process filedescriptor limit which is usually 1024-4096, and you could theoretically get a permission fail if the shell tried to reset the open flags, but dash, bash, and zsh don't appear to try to do this. I only get IO errors when I actually try to write to a duped readonly filedescriptor, not during the redirection. )