Subject: Re: discrepency beteen /bin/echo and builtin echo of /bin/sh
To: None <current-users@NetBSD.ORG>
From: Greg A. Woods <woods@weird.com>
List: current-users
Date: 06/09/2002 10:52:38
[ On Sunday, June 9, 2002 at 12:43:04 (+0100), Ben Harris wrote: ]
> Subject: Re: discrepency beteen /bin/echo and builtin echo of /bin/sh
>
> No. Any option aside from -n is specifically disallowed by both
> POSIX.2-1992 and the forthcoming corrigendum to POSIX-2001. This means that
> "echo -e foo" MUST send "-e foo\n" to stdout.
Yeah, and POSIX is god. I'd much rather stick to SuSv2 for anything
"real". At least it's more practical and pragmatic. POSIX is stuck in
the early 1980's and will undoutably stay there for decades to come,
esp. if ISO keeps oversight on it. POSIX never was practical, not ever,
However, I will note the copy of the D11.2 draft I have didn't go so far
as you say the final 1992 edition did. It only disallows '--'. It
allows any options, specifically un-defines the behaviour of '-n' as the
first option (and un-defines the behaviour of backslashes in any
strings), but doesn't require any options (nor does it require support
for backslashes).
Are you sure you're not mis-reading the 1992 edition?
[[ starting page 475, P1003.2/D11.2 1991 ]]
4.19 echo - Write arguments to standard output
4.19.1 Synopsis
echo [string ...]
4.19.2 Description
The echo utility shall write its arguments to standard output, followed
by a <newline> character. If there are no arguments, only the <newline>
character shall be written.
4.19.3 Options
The echo utility shall not recognize the -- argument in the manner
specified by utility syntax guideline 10 in 2.10.2; -- shall be
recognized as a string operand.
Implementations need not support any options.
4.19.4 Operands
The following operands shall be supported by the implementation:
string A string to be written to standard output. If the first
operand is "-n" or if any of the operands contain a
backslash (\) character, the results are implementation
defined.
4.19.5 External Influences
4.19.5.1 Standard Input
None.
4.19.5.2 Input Files
None.
4.19.5.3 Environment Variables
The following environment variables shall affect the execution of echo:
LANG This variable shall determine the locale to use for
the locale categories when both LC_ALL and the
corresponding environment variable (beginning with
LC_) do not specify a locale. See 2.6.
LC_ALL This variable shall determine the locale to be used
to override any values for locale categories
specified by the settings of LANG or any
environment variables beginning with LC_.
LC_MESSAGES This variable shall determine the language in which
diagnostic messages should be written.
4.19.5.4 Asynchronous Events
Default.
4.19.6 External Effects
4.19.6.1 Standard Output
The echo utility arguments shall be separated by single <space>s and a
<newline> character shall follow the last argument.
4.19.6.2 Standard Error
Used only for diagnostic messages.
4.19.6.3 Output Files
None.
4.19.7 Extended Description
None.
4.19.8 Exit Status
The echo utility shall exit with one of the following values:
0 Successful completion.
>0 An error occurred.
4.19.9 Consequences of Errors
Default.
BEGIN_RATIONALE
4.19.10 Rationale. (This subclause is not a part of P1003.2)
Examples,_Usage
As specified by this standard, echo writes its arguments in the simplest
of ways. The two different historical versions of echo vary in fatal
incompatible ways.
The BSD echo checks the first argument for the string "-n", which causes
it to suppress the <newline> character that would otherwise follow the
final argument in the output.
The System V echo does not support any options, but allows escape
sequences within its operands:
\a Write an <alert> character.
\b Write a <backspace> character.
\c Suppress the <newline> character that otherwise follows the
final argument in the output. All characters following the \c
in the arguments are ignored.
\f Write a <form-feed> character.
\n Write a <newline> character.
\r Write a <carriage-return> character.
\t Write a <tab> character.
\v Write a <vertical-tab> character.
\\ Write a backslash character.
\0num
Write an 8-bit value that is the 1-, 2-, or 3-digit octal number
num.
It is not possible to use echo portably across these two implementations
unless both -n (as the first argument) and escape sequences are omitted.
The printf utility (see 4.50) can be used to portably emulate any of the
traditional behaviors of the echo utility as follows:
- The System V echo is equivalent to:
printf "%b\n" "$*"
- The BSD echo is equivalent to:
if [ "X$1" = "X-n" ]
then
shift
printf "%s" "$*"
else
printf "%s\n" "$*"
fi
The echo utility does not support utility syntax guideline 10 because
existing applications depend on echo to echo all of its arguments, except
for the -n option in the BSD version.
New applications are encouraged to use printf instead of echo. The echo
utility has not been made obsolescent because of its extremely widespread
use in existing applications.
Copyright c 1991 IEEE. All rights reserved.
This is an unapproved IEEE Standards Draft, subject to change.
478 4 Execution Environment Utilities
--
Greg A. Woods
+1 416 218-0098; <gwoods@acm.org>; <g.a.woods@ieee.org>; <woods@robohack.ca>
Planix, Inc. <woods@planix.com>; VE3TCP; Secrets of the Weird <woods@weird.com>