Code:
while read g < list_1
The redirection is for the
read command.
Because it is within the while loop, it opens the file (and reads the 1st line) for each loop cycle.
In contrast, the following redirects the entire loop
Code:
while
read g
do
...
done < list_1
The shell opens the file once and redirects stdin.
Now each command that reads from stdin actually reads from the file. For each loop cycle the
read command reads a new line.
The file is closed when the loop terminates, at the end or by a
break.
For the advanced users:
the early Bourne shell allowed to switch order
And all shells are compatible.
But only the
zsh does, as a logical consequence, allow the same for a code block:
Code:
< list_1 while
read g
do
...
done
Not only while-do-done is a code block.
Also for-do-done and if-then-fi are code blocks.
And { } is an artificial code block. An example:
Code:
{
read line1
while
read line
do
...
done
} < inputfile
Again only
zsh allows
Code:
< inputfile {
...
}