Why should text files end with a newline?

Lately, one of my pet peeves is seeing files that aren’t terminated with a newline. It’s part of our coding standards for Calypso at Automattic and it’s configured into our editors. But, I never knew why until recently.

If you’re curious yourself, check out this StackOverflow article.

I assume everyone here is familiar with the adage that all text files should end with a newline. I’ve known of this “rule” for years but I’ve always wondered — why?

Because that’s how the POSIX standard defines a line:

3.206 Line
A sequence of zero or more non- characters plus a terminating character.
Therefore, lines not ending in a newline character aren’t considered actual lines. That’s why some programs have problems processing the last line of a file if it isn’t newline terminated.

Source: Why should text files end with a newline?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.