If I want to provide redirection for inputs or outputs (< myfile, or > myfile) for commands test-commands, consequent-commands, command1, and command2 inif test-commands; thenconsequent-commands;until test-commands; do consequent-commands; donewhile test-commands; do consequent-commands; donecommand1 && command2command1 || command2where shall I place the redirections?For example, I sawwhile read a;   do echo $a; done < myfile;I wonder why put < myfile after done, instead of after read a?That also makes me think more generally (as the beginning part of this post): where a direction shall be placed for a command appearing in a compound command?Does it matter whether the redirection is output or input?if I place the redirection at the end of the compound command, how can I know which component command in the compound command   the redirection applies to?Thanks.
Provide redirection to commands used in compound commands
bash
You can put redirections at any position in a simple command (i.e. invoking an alias, function, builtin or executable with parameters). On the other hand, if you want to apply a redirection to compound commands, the redirections have to come afterwards. This is how the shell syntax is designed. For example, the following are equivalent:<myinput mycommand argumentmycommand <myinput argumentmycommand argument <myinputwhereas the redirection can't be moved inwhile ; do ; done <myinput{ command1; command2; } <myinputYou can only apply a redirection to a compound command if it ends with a keyword or with punctuation. To apply redirection to something like foo && bar or foo; bar or foo | bar, put the command in braces, e.g.{ foo && bar; } <myinputThe scriptswhile read a; do  echo $adone <myfileandwhile read a <myfile; do  echo $adoneare both syntactically correct, but the second one doesn't do anything useful. A redirection operator on a command means:Open the specified file.Attach the specified file to the requisite file descriptor (e.g. 0 for <).Run the command.Close the file descriptor.So read a <myfile opens the file again on each iteration, which means that it keeps reading the first line forever.If you want to use a while-read loop and want to keep the surrounding standard input for the loop body, read from a different file descriptor.while read a <&3; do  # commands that can use stdindone 3<myfileThe redirection operator <&3 merely does file descriptor duplication, it doesn't open the file again in the enumeration above, there's no step 1.