Double redirection test
What does this print your shel开发者_运维百科l?
echo foo | while read line; do echo $line; done < <(echo bar)
I would expect it to evaluate to echo foo | bar
or foo < <(bar)
, both of which would result in an error message.
In Bash 4.1.5 it looks like the pipe is simply discarded:
bar
In Dash:
sh: Syntax error: redirection unexpected
Dash doesn't support process substitution (<()
).
The behavior you're seeing is consistent if you use syntax that's supported by each of the shells you're comparing. Try this:
echo hello | cat < inputfile
You should see the contents of "inputfile" and not "hello". Of several shells I tried, only Z shell showed both.
This is what POSIX says regarding pipelines and redirection:
The standard output of command1 shall be connected to the standard input of command2. The standard input, standard output, or both of a command shall be considered to be assigned by the pipeline before any redirection specified by redirection operators that are part of the command (see Redirection ).
I interpret this to mean that in the case of the example above, the pipeline assigns stdin to cat
then the redirection overrides it.
精彩评论