Look at the following implementations of the "echo" command:
http://bxr.su/o/bin/echo/echo.c (OpenBSD)
http://bxr.su/d/bin/echo/echo.c (DragonFly)
http://bxr.su/n/bin/echo/echo.c (NetBSD)
http://bxr.su/f/bin/echo/echo.c (FreeBSD)
http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/echo.c (GNU)
As you go down the list, I'm sure you'll notice the increasing bloat in each implementation. What is the point of a 272 line echo program?
In their article 'Program Design in the UNIX Environment', Pike & Kernighan discuss how the
cat
program accreted control arguments. Somewhere, though not that article, there was a comment about 'cat
came back from Berkeley waving flags'. This is a similar issue to the problem withecho
developing options. (I found a reference to the relevant article in the BSD (Mac OS X) man page forcat
: Rob Pike, "UNIX Style, or cat -v Considered Harmful", USENIX Summer Conference Proceedings, 1983. See also http://quotes.cat-v.org/programming/)In their book 'The UNIX Programming Environment', Kernighan & Pike (yes, those two again) quote Doug McIlroy on the subject of what 'echo' should do with no arguments (circa 1984):
The UNIX and the Echo
The Korn shell introduced (or, at least, included) a
printf
command that was based on the C languageprintf()
function, and that uses a format string to control how the material should appear. It is a better tool for complicated formatting thanecho
. But, because of the history outlined in the quote,echo
doesn't just echo any more; it interprets what it is given to echo.And interpreting the command line arguments to
echo
indubitably requires more code than not interpreting them. A basic echo command is:There are other ways to achieve that. But the more complex versions of
echo
have to scrutinize their arguments before printing anything - and that takes more code. And different systems have decided that they want to do different amounts of interpretation of their arguments, leading to different amounts of code.I'm not sure if I like the first implementation: it has one option too many!
If a
-n
option was required, then also adding a--
option to stop option processing would be helpful. That way if you are writing a shell script that reads and prints user input, you don't get inconsistent behavior if the user types-n
.You will notice there is not really that much bloat growth.
Also, most of the time, you're not even running the echo command, most of the time 'echo' invokes a shell built-in. At least on my machine, you have to type
/bin/echo --help
to get all the advanced functionality/documentation out of it, becauseecho --help
merely echo's--help
For a good example, run this in your shell.
Then run this:
And you will note vastly different results.
The former will just emit the input as-is, but the latter will print
hello
coloured red.Another example, using the 'hex' code:
Vastly different behaviour. The openbsd implementation cannot to this =).