Alex Rivera | Logout

Bloated echo command

Asked 2010-07-20T13:57:48.907
9

Look at the following implementations of the "echo" command:

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?

Edit
Report

1 Answer

8

You will notice there is not really that much bloat growth.

  1. Most of the lines of code are comments.
  2. Most of the lines of code that are not comments, are usage documentation, so when somebody goes 'echo --help' it will do something.
  3. Code outside of the above appears largely to be handling the arguments echo can take, as well as the "special" expansion for symbols such as \n and \t to be their equivalent characters instead of echoing them literally.

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, because echo --help merely echo's --help

For a good example, run this in your shell.

 echo '\e[31mhello\e[0m'

Then run this:

 echo -e '\e[31mhello\e[0m'

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:

$echo '\x64\x65\x66'
\x64\x65\x66

$echo -e '\x64\x65\x66'
def

Vastly different behaviour. The openbsd implementation cannot to this =).

answered 2010-07-20T14:01:36.897

Your Answer