What could be more simple?
bash-2.05a$ echo foo foo
Well, actually it does get more complicated. The first problem is the interpretation of escape characters. System V Unixes interpret these by default, like this:
bash-2.05a$ echo "b\bcook" cook
Linux and BSD systems do not unless you use -e:
bash-2.05a$ echo "b\bcook" b\bcook bash-2.05a$ echo -e "b\bcook" cook
(echo is often a shell built in, but that doesn't affect this discussion for most shells).
Many System V shell scripts will use \c to suppress a new line:
apl$ echo "hello \c"; echo "there" hello there
That won't work on Linux or BSD unless you "echo -e" or discard the \c and use echo -n instead.
The man page for Linux and BSD echo includes the apparently useless -E flag, which disables interpretation of escape characters. As that is the default, what is it needed for? Well, although few sites do this, you CAN compile echo with an option that reverses the default, and (with bash) you can use "shopt" to set xpg_echo (shopt -s xpg_echo):
bash-2.05a$ shopt -s xpg_echo bash-2.05a$ echo "b\bcook" cook bash-2.05a$ /bin/echo "b\bcook" b\bcook bash-2.05a$ echo -E "b\bcook" b\bcook
Got something to add? Send me email.
More Articles by Tony Lawrence © 2011-07-05 Tony Lawrence