3

What does this print your shell?

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
l0b0
  • 55,365
  • 30
  • 138
  • 223
  • Why does it matter? It is bad design, you shouldn't redirect standard input more than once. – Benoit Jan 07 '11 at 14:31
  • It was more of a mental exercise - I just realized that it was valid syntax, and that it should do something "reasonable" with it. – l0b0 Jan 07 '11 at 14:39

1 Answers1

3

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.

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439