|
Home > Archive > Unix Programming > July 2007 > echo and getopt prohibition
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
echo and getopt prohibition
|
|
| Bob Nelson 2007-07-22, 7:17 pm |
| In the BSD (Open, Net and Free) ``echo.c'' source, the following comment
appears:
/* This utility may NOT do getopt(3) option parsing. */
What prevents ``getopt'' from being used in the ``echo'' command?
| |
| Roger Leigh 2007-07-22, 7:17 pm |
| | |
| noogie.brown@gmail.com 2007-07-22, 7:17 pm |
| On Jul 22, 8:15 pm, Bob Nelson <bnel...@nelsonbe.com> wrote:
> In the BSD (Open, Net and Free) ``echo.c'' source, the following comment
> appears:
>
> /* This utility may NOT do getopt(3) option parsing. */
>
> What prevents ``getopt'' from being used in the ``echo'' command?
At a guess, maybe something to do with echo being an important tool
and it being available on a minimal install, perhaps where getopt
isn't available.
| |
| Henry Townsend 2007-07-23, 1:19 am |
| Bob Nelson wrote:
> In the BSD (Open, Net and Free) ``echo.c'' source, the following comment
> appears:
>
> /* This utility may NOT do getopt(3) option parsing. */
>
> What prevents ``getopt'' from being used in the ``echo'' command?
I would expect that it's because getopt swallows certain standard
sequences such as "--". In other words according to POSIX "echo -- foo"
must print "-- foo" but if getopt() was called it would print "foo".
This is purely speculative; I have not written a test.
| |
| Geoff Clare 2007-07-23, 1:21 pm |
| Roger Leigh wrote:
> Bob Nelson <bnelson@nelsonbe.com> writes:
>
>
> Nothing. GNU echo (coreutils) as well as the bash echo builtin
> support options (-e, -n etc.). There might be some ancient reason for
> the BSD behaviour, but it's certainly neither universal nor required.
It is required by POSIX:
"The echo utility shall not recognize the "--" argument in the
manner specified by Guideline 10 of the Base Definitions volume of
IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines;
"--" shall be recognized as a string operand.
Implementations shall not support any options.
[...]
If the first operand is -n, or if any of the operands contain a
backslash ('') character, the results are implementation-defined."
Note that it says "operand" when referring to -n, not "option".
POSIX requires "echo -e foo" to output "-e foo", and as far as I'm
aware GNU echo and the builtin echo in bash are the _only_ versions
of echo that do not conform to this POSIX requirement.
--
Geoff Clare <netnews@gclare.org.uk>
| |
| Alan Curry 2007-07-23, 1:21 pm |
| In article <7pken4-208.ln1@leafnode-msgid.gclare.org.uk>,
Geoff Clare <netnews@gclare.org.uk> wrote:
>
>POSIX requires "echo -e foo" to output "-e foo", and as far as I'm
>aware GNU echo and the builtin echo in bash are the _only_ versions
>of echo that do not conform to this POSIX requirement.
zsh builtin echo doesn't either, even in "emulate sh" mode with BSD_ECHO
turned on.
--
Alan Curry
pacman@world.std.com
|
|
|
|
|