The process substitution
<(cat) doesn’t work. Why?
head <(cat) cat: -: Input/output error
I can use an extra file descriptor, but I don’t understand why the above doesn’t work.
# Using file descriptor 3 to reroute the input (head <(cat <&3)) 3<&0
Here is Solutions:
We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.
bash, if you do:
$ cat <(ps -j) PID PGID SID TTY TIME CMD 3887 16480 16480 pts/29 00:00:00 bash 3888 3888 16480 pts/29 00:00:00 cat 3889 16480 16480 pts/29 00:00:00 ps 16480 16480 16480 pts/29 00:00:00 bash
$ cat <(ps -j) PID PGID SID TTY TIME CMD 3935 3935 16480 pts/29 00:00:00 ps 3936 3936 16480 pts/29 00:00:00 cat 16480 16480 16480 pts/29 00:00:00 zsh
$ cat <(ps -j) PID PGID SID TTY TIME CMD 3946 16480 16480 pts/29 00:00:00 ps 3947 3947 16480 pts/29 00:00:00 cat 16480 16480 16480 pts/29 00:00:00 ksh
In all 3 shells, the
ps process is in a different process group from
cat‘s one, which is the foreground process group of the terminal.
zsh at least is nice enough to redirect stdin in there to
/dev/null if it was a tty to avoid problems with that, like many shells do for commands run in background.
Your command would work OK if stdin was not a terminal, but here, since
cat is not in the foreground process group of the terminal, it reading from the terminal means it will receive a
SIGTTIN signal which would cause it to be suspended. And that is not handled gracefully here. In your case, it seems
SIGTTIN is being ignored or blocked for you to get a EIO error (which you get when trying to read from your controlling terminal when you’re not in its foreground process group and ignore/block
(head <(cat <&3)) 3<&0
however, we’re starting a sub-shell in foreground, and all the processes in there end up in that same process group so are allowed to read from the terminal. With the explicit redirection in there, with
zsh, we’re bypassing
zsh‘s redirection from
/dev/null. With other shells,
would also work.
Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂