Missing The Last Line in a File

suggest change

The C standard says that files should end with a new line, so if EOF comes at the end of a line, that line may not be missed by some commands. As an example:

$ echo 'one\ntwo\nthree\c' > file.txt$ cat file.txtonetwothree$ while read line ; do echo "line $line" ; done < file.txtonetwo

To make sure this works correctly for in the above example, add a test so that it will continue the loop if the last line is not empty.

$ while read line || [ -n "$line" ] ; do echo "line $line" ; done < file.txt

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents