3 ms·
I don't know what you mean by "forward" but I have the impression you don't understand how file descriptors (i.e. "open files or streams") work. The shell sets
by jstimpfle 8y ago
I don't know what you mean by "forward" but I have the impression you don't understand how file descriptors (i.e. "open files or streams") work.
The shell sets up a pipe and connects the left side to the pipe's writing end, and the right side to the reading end. Now, the right side is actually a subshell (indicated by the parentheses "(...)"). And that subshell can spawn as many other processes, sequentially or in parallel, as it wants. All of them will get the open file descriptor (the pipe's reading end) inherited by the operating system.
If you had multiple processes in parallel reading from the pipe, the outcome would be totally nondeterministic (dependent on the kernel's scheduling behaviour. In the example case, none of the potential readers actually read (not the subshell itself, not the sleep, and not the echo).
Here's a perhaps illuminating example:
(echo h; echo hello; echo HALLO; ) | ( read firstline; echo "Firstline is $firstline"; grep A; )
Does that help?