Re: Confusing bash behaviour



On 08/09/10 16:17, Vilmos Soti wrote:
Hello,

This is a solved problem for me, but I think people should be aware of
this strange and confusing bash behaviour.

for x in `command`; do command-list; done
command | while read x; do command-list; done

are not interchangeable.

The problem arises if in the command-list there is a command which reads
from stdin. In that case, the for loop behaves as expected, but in the
case of a while loop, the stdin reading program will consume some or
all of the remaining lines fed into the while loop.

Correct. In the first example "command" is run to completion and it's
output placed onto the for command line.
In the second example "command" feeds one line at a time into the pipe
which reads one with "read". The rest are available and can be consumed
by command-list.
.