Reputation: 11
Can someone please explain how these process substitutions are working.
(echo "YES")> >(read str; echo "1:${str}:first";)> >(read sstr; echo "2:$sstr:two")> >(read ssstr; echo "3:$ssstr:three")
Output
1:2:3:YES:three:two:first
I've figured out, that the 'ssstr'-Substitution got FD 60, sstr FD 61 and str FD 62. (right to left)
But how is (echo "YES") connected to input of FD60, and output of FD60 with input FD61 and so on and finally FD62 prints out on Terminal ?
All against the direction of the two redirections.
How are they nested, and how connected ? Makes me crazy. Ty.
Upvotes: 1
Views: 118
Reputation: 531115
First off, don't actually write code like this :)
The process substitutions are the constructs >(...)
. The (...)>
isn't a specific construct; it's just a subshell followed by an output redirection.
This example is a single command (echo "YES")
followed by three output redirections
> >(read str; echo "1:${str}:first";)
> >(read sstr; echo "2:$sstr:two")
> >(read ssstr; echo "3:$ssstr:three")
The last one is the one that is actually applied to the original command; something like echo word >foo >bar >baz
would create all three files, but the output of echo
would only be written to baz
.
Similarly, all three process substitutions start a new process, but the output YES
is only written to the last one. So read ssstr
gets its input from echo YES
.
At this point, I think you are seeing what amounts to undefined behavior. The three process substitutions run in the reverse order they were created, as if the OS pushed each process on to a stack as the next one is created, then schedules them by popping them off the stack, but I don't think that order is guaranteed by anything.
In each case, though, the standard input of each process substitution is fixed to the standard output of the command, which is whichever other process substitution just ran. In other words, the command ends up being similar to
echo YES | {
read ssstr
echo "3:$ssstr:three" | {
read sstr
echo "2:$sstr:two" | {
read str
echo "1:$str:one"
}
}
}
Upvotes: 2