Having a newline character at end of file

Tagged As:

Background/problem/question:
When you create a newfile, and push it to Github, in code diff view, it will say no newline at end of file. Sometimes text editors would also add an EOF newline, if they noticed that the file being edited does not end in a newline, when they are saved (usually it is configured that way by default).

Reasoning:
In POSIX, a line is a sequence of zero or more non-newline characters plus a terminating <newline> character. Therefore some Unix tools would expect text files to end with a newline character, especially when they process text line by line.
For example ‘wc’ will expect every line to end with a newline.

test.txt:

first line
second line

Then…

$ wc -l test.txt
1 test.txt

We only get one line.

Conclusion:
This is only a concern. The modern platforms and applications we use should be ok for a text file not to end with a newline. However as long as we use the tools that would expect each line to end with a newline, we might not see all the results.

References/Resources: