Automation, Orchestration, and Scripting

In Bash, when does the loop while read -r line; do echo "$line"; done < hosts.txt stop?

Show the answer

Correct answerA. When read returns a nonzero status at end of file, after one pass per line

AWhy this is right: read succeeds for each line and fails at end of file, which ends the while loop.

BWhy not this: read returns 0 for a blank line and sets line to an empty string, so the loop goes on.

CWhy not this: The redirection after done feeds the whole file to the loop, and read takes one line per pass.

DWhy not this: while tests the condition command, read, not the commands in the body.

The read builtin returns 0 when it reads a line and nonzero at end of file, so the while condition fails and the loop ends. The redirection after done feeds the file to the loop's standard input.