Alex Rivera | Logout

Why should text files end with a newline?

Asked 2009-04-08T12:16:39.483
2131

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?

Edit
Report

3 Answers

2025

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

3.206 Line
A sequence of zero or more non- <newline> characters plus a terminating <newline> character.

Therefore, “lines” not ending in a newline character aren't considered actual lines. That's why some programs process the last line of a file differently if it isn’t newline-terminated.

The advantage of following this convention is that it is internally consistent and all POSIX tools expect and use it. For instance, when concatenating files with cat, a file terminated by newline (a.txt and c.txt below) will have a different effect than one without (b.txt):

$ more a.txt
foo

$ more b.txt
bar
$ more c.txt
baz

$ cat {a,b,c}.txt
foo
barbaz

We follow this rule for consistency. Doing otherwise would incur extra work when dealing with the default POSIX tools.


Think about it differently: If lines aren’t terminated by newline, making commands such as cat useful is much harder: how do you make a command to concatenate files such that

  1. it puts each file’s start on a new line, which is what you want 95% of the time; but
  2. it allows merging the last and first line of two files, as in the example above between b.txt and c.txt?

Of course this is solvable but you need to make the usage of cat more complex (by adding positional command line arguments, e.g. cat a.txt --no-newline b.txt c.txt), and now the command rather than each individual file controls how it is pasted together with other files. This is almost certainly not

answered 2009-04-08T12:46:40.113
3

I was always under the impression the rule came from the days when parsing a file without an ending newline was difficult. That is, you would end up writing code where an end of line was defined by the EOL character or EOF. It was just simpler to assume a line ended with EOL.

However I believe the rule is derived from C compilers requiring the newline. And as pointed out on “No newline at end of file” compiler warning, #include will not add a newline.

answered 2009-07-01T17:10:42.790
-10

IMHO, it's a matter of personal style and opinion.

In olden days, I didn't put that newline. A character saved means more speed through that 14.4K modem.

Later, I put that newline so that it's easier to select the final line using shift+downarrow.

answered 2009-04-08T12:38:48.820

Your Answer