I usually use the read command to read an input file to the shell script line by line. An example code such as the one below yields a wrong result if a new line isn\'t inser
Use while loop like this:
while IFS= read -r line || [ -n "$line" ]; do
echo "$line"
done
Or using grep with while loop:
while IFS= read -r line; do
echo "$line"
done < <(grep "" file)
Using grep . instead of grep "" will skip the empty lines.
Note:
Using IFS= keeps any line indentation intact.
You should almost always use the -r option with read.
Don't read lines with for
File without a newline at the end isn't a standard unix text file.