For
- If you cat a file without a trailing newline your prompt may appear at the end of the output instead of flush left.
- Some C++ compilers complain when there is no trailing newline.
- Each line in the file ends in a newline.
wc -l
reports an incorrect result if it is missing- More reasons
Against
- git considers a trailing newline a whitespace error.
- Trailing whitespace is easily lost when, for example, exchanging patches via email.
- A newline after a closing “?>” in PHP can cause problems with
header()
(although closing tags are no longer required in PHP). - It uses one more byte.
For: POSIX compliance
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html
3.205 Line
A sequence of zero or more non- characters plus a terminating character.
3.395 Text File
A file that contains characters organized into zero or more lines. The lines do not contain NUL characters and none can exceed {LINE_MAX} bytes in length, including the character. Although POSIX.1-2008 does not distinguish between text files and binary files (see the ISO C standard), many utilities only produce predictable or meaningful output when operating on text files. The standard utilities that have such restrictions always specify “text files” in their STDIN or INPUT FILES sections.