Search code examples
shellloopsio-redirectionifs

reading a file line by line using the shell


the correct syntax to read a .txt file line by line is:

while IFS= read -r line
do
    echo "$line"
done < input_file

I don't understand why it works fine when we put <input_file after done and go in an infinite loop printing the 1st line of input_file when we put <input_file after -r line in the 1st line shouldn't they be the same?


Solution

  • while IFS= read -r line < input_file
    do
        echo "$line"
    done
    

    In this scenario, you are re-initiating the read of input_file with every iteration of the loop. As read reads line by line, each re-initialisation will print the first line. As you never progress past this first line, it will print the first line only indefinitely

    while IFS= read -r line
    do
        echo "$line"
    done < input_file
    

    In this scenario, the input_file is redirected in to the while loop and then the read line takes place with each line of the redirected output until it gets to the end of the output and then end of the file.

    An alternate way of processing the file is to cat file_input through to the while loop:

    cat input_file | while IFS= read -r line
    do
        echo "$line"
    done
    

    This works in the same way as the previous section but is less efficient due to the pipe. The flow of the output runs from left to right as opposed to right to left. This is often easier to understand. The output of cat input_file is fed (piped) into the while loop and each line is read till the end of the output/file.