I’ve been getting back to the basics lately. The following excerpt is from opengroup.org.
echo is out.
printf is in. I think I remember learning from my mentor, Stu, about 10 years ago…
But instead of integrating this somber fact, I invented all sorts of silly reasons to not adopt
echo. One less character. Easier to type. Sounds cooler. Is actually a word. I like delay pedals. Whatever. I wish I had gotten curious about my beliefs instead of rationalizing towards whatever I wanted to be true.
It is not possible to use
echoportably across all POSIX systems unless both
-n(as the first argument) and escape sequences are omitted.
printfutility can be used portably to emulate any of the traditional behaviors of the
echoutility as follows (assuming that IFS has its standard value or is unset):
- The historic System V
echoand the requirements on XSI implementations in this volume of IEEE Std 1003.1-2001 are equivalent to:
printf "%b\n" "$*"
- The BSD
echois equivalent to:
if [ "X$1" = "X-n" ] then shift printf "%s" "$*" else printf "%s\n" "$*" fi
New applications are encouraged to use
echoutility has not been made obsolescent because of its extremely widespread use in historical applications. Conforming applications that wish to do prompting without
<newline>s or that could possibly be expecting to
-n, should use the
printfutility derived from the Ninth Edition system.
echowrites its arguments in the simplest of ways. The two different historical versions of
echovary in fatally incompatible ways.
echochecks the first argument for the string
-nwhich causes it to suppress the
<newline>that would otherwise follow the final argument in the output.
The System V
echodoes not support any options, but allows escape sequences within its operands, as described for XSI implementations in the OPERANDS section.
echoutility does not support Utility Syntax Guideline 10 because historical applications depend on
echoall of its arguments, except for the
-noption in the BSD version.