4 ms·
Why is newline at the end relevant? I remember not having a newline breaks some tools... but why? It can't be because of unix philosophy!?
by jve 2y ago
Why is newline at the end relevant?
I remember not having a newline breaks some tools... but why? It can't be because of unix philosophy!?
- mook 2y agoSome old tools had bugs where they'd read a line (up to the new line) and then process it, so if the last line didn't end with a new line they'd never do the processing. So a manual workaround for bugs became the convention.
- paulv 2y agoBack in ~2002 this was the case with cron. Found out the hard way when all the backup tapes we desperately needed were completely empty.
- bityard 2y agoNo, that is not a bug and there was never such a thing as "manually" adding a trailing newline. All tools add newlines automatically where they should be. Unix has ALWAYS defined a line of text as being terminated by a newline. The last line in the file is not an exception.
- SSLy 2y agoso that you can cat(1) multiple files at once, and their bookends don't get glued.
- bityard 2y agoUnix's first job in life was as a documentation processing system. It was made to be very good at dealing with text. All of the tools which process text expect every line to end in a newline. The last line is not exempt from this. All classic Unix text editors will automatically append a trailing newline to any text file you create with them. Some modern tools may be _tolerant_ of omitted trailing newlines, but you shouldn't rely on that. A text file should always have a newline as its last character. Otherwise, it's not _really_ a text file. POSIX defines it more succinctly than I do: A text file contains one or more "lines" and every "line" is terminated by a "newline." https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_2 https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1... https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_403 https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1...