Why is the first input being skipped

Viewed 56
#!/bin/bash

while read line;
do
    cat
done

Hi, I'm trying to get the program to print each line given from stdin. Why isn't the first input being printed here? 2nd and following inputs work fine.

Thanks

Edit: I fixed it by adding a cat before the loop. Can someone explain why it was needed though?

1 Answers
#!/bin/bash

while read -r line;
do
    echo "${line}"
done

This is a working version of your script. In your script, the first line is placed in the variable ${line} by the bash builtin read. Since you only ever output via cat, the first line in that variable is never output.

Related