Quote:
Originally Posted by
tukuyomi
\n works on sh shell at least, but truly, I tested on bash and it does not output the new line
There was a major difference in the behavior of the
echo utility (or shell built-in) between 4.2BSD (from the University of California, Berkeley) and UNIX System V (from AT&T Bell Laboratories). In the UNIX System V
echo there were no options, but the common backslash character (
\) escapes for control characters were recognized and, in addition,
\c suppressed the <newline> that otherwise follows the final argument in the output and ignores all characters following the
\c in its arguments. In the 4.2BSD
echo backslashes in the arguments were printed as is and there was one option (
-n) that suppressed the trailing <newline>.
When the POSIX.2 Shell and Utilities standard was being written, this disparity in behavior was one of the two key disagreements between committee members. It was resolved by inventing the
printf utility and leaving the behavior of
echo unspecified if its 1st argument is
-n or any argument contains a backslash. It is also one of the extremely few utilities that is not allowed to treat
-- as an end of options indicator and is not allowed to have any options other than
-n. (The default GNU echo violates these requirements.)
Since The Open Group's
X/Open Portability Guide was combined with the POSIX standards by The Austin Group (making the POSIX Standards and The
Single UNIX Specifications a single document with three different covers: one for The Open Group's Single UNIX Specifications, one for the
IEEE Std. 1003 POSIX Standards, and one for the
ISO/IEC IS 9945 POSIX Standards), systems claiming to support the POSIX X/Open System Interfaces option (with the XSI margin marking) are required to provide an
echo that behaves the way UNIX System V echo behaved. POSIX conforming implementations that do not claim to support the XSI option are allowed to provide an
echo that behaves the way the 4.2BSD
echo behaved or the way the UNIX System V
echo behaved.
When trying to write portable applications, use
printf rather than
echo if the first argument to
echo could start with a hyphen (
-) or if any of its arguments could contain a backslash.
Hope this helps,
Don
PS. The other key disagreement between the Berkeley and AT&T camps when developing the original POSIX.2 Standard was over
cpio versus
tar which lead to the invention of the
pax utility. It is not a coincidence that pax is the Latin word for the English word peace.
This concludes today's ancient history lesson.