I sometimes whether I should put a newline after the last line in a configuration file, especially if it is a one-line configuration file, like the hostname file.
Does it not matter, or is there a rule?
I sometimes whether I should put a newline after the last line in a configuration file, especially if it is a one-line configuration file, like the hostname file.
Does it not matter, or is there a rule?
Yes, the last character of a valid POSIX text file must be a newline.
It has been debated before.
It is easy (and very fast) to correct a file (if needed).
And vim might be configured to automatically ensure that a trail newline is included.
It matters in the sense that some tools expect a text file, and therefore expect an ending newline. For example the shell read
will fail (unless configured in a special way) to read the last line of a text file that is missing a newline. And concatenation of files with cat
will also require special handling.
You can not follow the recomendations to reduce one byte per file, but at the cost of added complexities to process files. It is not worth the trouble IMO.
I can not think of any other reason to avoid having an ending newline for a text file.