Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
The Unix and the Echo (2001) (everything2.com)
28 points by charlysl on May 15, 2020 | hide | past | favorite | 8 comments


If you have a file consisting of some text, the file might or might not end with a \n

If you have a file consisting of some lines of text, the file will end with a \n because a line of text (unlike text generally) is typically defined by matching ^[^\n]*\n

Therefore, `echo` with no arguments returns a line of text which contains nothing.


I think you are right, it is a matter of convenience, to type less, as the story hints, but not only for writing to files, also for displaying messages on the console.

According to the book quoted in the link:

Since a command should by default execute its most commonly used function, ... echo appends the final newline automatically.

It may seem insignificant, but this story highlights, in nerd humour, some of the design philosophy behind Unix.

Classic Kernighan (as the book's co-author).


Notably, reading lines of text with bash's "while read" will skip the last line if the last line does not end in \n.


"echo -n" will omit the trailing newline, on both Linux and BSD. echo -n with no other arguments therefore outputs literally nothing.

(Except for the "echo" builtin in /bin/sh, which does not recognise -n.)


Debian GNU/Linux guarantees that the echo builtin, even in /bin/sh, supports the -n option:

https://www.debian.org/doc/debian-policy/ch-files.html#scrip...


> f u cn rd ths u mst uz unix

I thought this was cursing at me at first!

By the way, if you actually care about your newlines use printf.


Everything2 rocks. I used to be a somewhat prolific noder.


I'd say they overthought the behavior of echo... just a little bit.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: