The following line is obvious:
echo "bla" | foo | bar
But does the ones below do the same?
echo "bla" | bar <(foo)
echo "bla" | bar < <(foo)
Which of the foo
and bar
read "bla" from stdin and why?
I mean that, of course, I can just code it and check it, but I'm not sure if it's defined behavior or I'm exploiting features I should not rely on.
Best Answer
That's shell dependant and not documented AFAICS. In
ksh
andbash
, in the first case,foo
will share the same stdin asbar
. They will fight for the output ofecho
.So for instance in,
You see evidence that
paste
reads every other block of text fromseq
's output whiletr
reads the other ones.With
zsh
, it gets the outer stdin (unless it's a terminal and the shell is not interactive in which case it's redirected from/dev/null
).ksh
(where it originated),zsh
andbash
are the only Bourne-like shells with support for process substitution AFAIK.In
echo "bla" | bar < <(foo)
, note thatbar
's stdin will be the pipe fed by the output offoo
. That's well defined behaviour. In that case, it appears thatfoo
's stdin is the pipe fed byecho
in all ofksh
,zsh
andbash
.If you want to have a consistent behaviour across all three shells and be future-proof since the behaviour might change as it's not documented, I'd write it:
To be sure
foo
's stdin is also the pipe fromecho
(I can't see why you'd want to do that though). Or:To make sure
foo
does not read from the pipe fromecho
. Or:To have
foo
's stdin the outer stdin like in current versions ofzsh
.