While trying to learn how to manipulate the content of files in bash, I encountered the following code example:

while IFS=, read -r col1 col2
    echo "I got:$col1|$col2"
done < myfile.csv

According to The Open Group Base Specifications Issue 6:

The read utility shall read a single line from standard input.

If my understanding is correct, this means that, for instance, if I want read to read lines from myfile.csv, I should add < myfile.csv to the end of the read command, such as:

read -r col1 col2 < myfiles.csv

However, in the annexed code, < myfile.csv is appended after the done keyword. Why is that?

1 Answer 1


For the purposes of redirection in this example, stdin for everything in the while loop, including the conditional, will be myfile.csv

You could redirect it as you suggest, but then the redirection would be set up separately for each call to read, and it would just read the first line every time.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .