Versions of Echo Vary in Fatally Incompatible Ways
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 printf over 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.
APPLICATION USAGE
It is not possible to use
echoportably across all POSIX systems unless both-n(as the first argument) and escape sequences are omitted.The
printfutility can be used portably to emulate any of the traditional behaviors of theechoutility 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" "$*" fiNew applications are encouraged to use
printfinstead ofecho.RATIONALE
The
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 toechoa-n, should use theprintfutility derived from the Ninth Edition system.As specified,
echowrites its arguments in the simplest of ways. The two different historical versions ofechovary in fatally incompatible ways.The BSD
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.The
echoutility does not support Utility Syntax Guideline 10 because historical applications depend onechotoechoall of its arguments, except for the-noption in the BSD version.