This is odd. I had seen the echo.c from some BSD system in the past. I don't recall which it was --- OpenBSD, FreeBSD, NetBSD. But I think it
was OpenBSD because OpenBSD's source still behaves I remember it.
Here's the behavior.
%echo -n -n hello
hello%
I've looked at all revisions there. They're all essentially the same.
The behavior written in C in the URL above is essentially this:
%./openbsd-echo -n -n hello
-n hello%
So, what's up with that?
This is odd. I had seen the echo.c from some BSD system in the past. I don't recall which it was --- OpenBSD, FreeBSD, NetBSD. But I think it
was OpenBSD because OpenBSD's source still behaves I remember it.
Here's the behavior.
%echo -n -n hello
hello%
I remember this well because when I read it for the first time, there
was a loop looking for -n, which prompted me to ask --- omg, how am I
going to print ``-n'' to the terminal?
Meredith Montgomery <mmontgomery@levado.to> writes:
This is odd. I had seen the echo.c from some BSD system in the past. I
don't recall which it was --- OpenBSD, FreeBSD, NetBSD. But I think it
was OpenBSD because OpenBSD's source still behaves I remember it.
Here's the behavior.
%echo -n -n hello
hello%
[...]
Both the GNU /bin/echo and the bash builtin also behave in this way. The (d)ash one doesn't.
I remember this well because when I read it for the first time, there
was a loop looking for -n, which prompted me to ask --- omg, how am I
going to print ``-n'' to the terminal?
The solution to this is usually 'use printf(1)' which behaves in a sane
way.
Rainer Weikusat <rweikusat@talktalk.net> writes:
Meredith Montgomery <mmontgomery@levado.to> writes:
This is odd. I had seen the echo.c from some BSD system in the past. I >>> don't recall which it was --- OpenBSD, FreeBSD, NetBSD. But I think it
was OpenBSD because OpenBSD's source still behaves I remember it.
Here's the behavior.
%echo -n -n hello
hello%
[...]
Both the GNU /bin/echo and the bash builtin also behave in this way. The
(d)ash one doesn't.
Why would anyone do an echo program like that? Where they initially
thinking that they would support lots of flags and therefore parse the
entire argv?
John McCue <jmccue@fuzzball.mhome.org> writes:
Meredith Montgomery <mmontgomery@levado.to> wrote:
This is odd. I had seen the echo.c from some BSD system in the past. I >>> don't recall which it was --- OpenBSD, FreeBSD, NetBSD. But I think it<snip>
was OpenBSD because OpenBSD's source still behaves I remember it.
Here's the behavior.
%echo -n -n hello
hello%
I've looked at all revisions there. They're all essentially the same.
The behavior written in C in the URL above is essentially this:
%./openbsd-echo -n -n hello
-n hello%
So, what's up with that?
/bin/echo and builtins with csh(1), tcsh(1)and ksh(1)
on OpenBSD prints:
-n hello
Are you sure it was not some builtin from another or older
shell ?
It was a builtin. I was totally fooled by it. For years because for
years I've been thinking that some BSD echo was weird. I'm curious now
as to why GNU echo loops through argv. (The only thing I can think of
is that it was plans for the future --- new flags and so on. But POSIX requires only -n and as a first argument --- IIRC.)
Meredith Montgomery <mmontgomery@levado.to> wrote:
This is odd. I had seen the echo.c from some BSD system in the past. I<snip>
don't recall which it was --- OpenBSD, FreeBSD, NetBSD. But I think it
was OpenBSD because OpenBSD's source still behaves I remember it.
Here's the behavior.
%echo -n -n hello
hello%
I've looked at all revisions there. They're all essentially the same.
The behavior written in C in the URL above is essentially this:
%./openbsd-echo -n -n hello
-n hello%
So, what's up with that?
/bin/echo and builtins with csh(1), tcsh(1)and ksh(1)
on OpenBSD prints:
-n hello
Are you sure it was not some builtin from another or older
shell ?
Meredith Montgomery <mmontgomery@levado.to> writes:
Rainer Weikusat <rweikusat@talktalk.net> writes:
Meredith Montgomery <mmontgomery@levado.to> writes:
This is odd. I had seen the echo.c from some BSD system in the past. I >>>> don't recall which it was --- OpenBSD, FreeBSD, NetBSD. But I think it >>>> was OpenBSD because OpenBSD's source still behaves I remember it.
Here's the behavior.
%echo -n -n hello
hello%
[...]
Both the GNU /bin/echo and the bash builtin also behave in this way. The >>> (d)ash one doesn't.
[...]
Why would anyone do an echo program like that? Where they initially
thinking that they would support lots of flags and therefore parse the
entire argv?
Compatibility with existing code, most likely. Judging from comments in
the GNU echo.c, they were aiming for compatibility with the /bin/sh
builtin echo of V9 Research UNIX said to be distributed as part of
SysV.
Apart from that, that's the usual convention: Silently process any number of repeated options. For boolean options, that's not going to change
anything. OTOH, one has to handle this situation somehow and this
approach is easy to implement and semantically perfectly reasonable
(enabling something that's already enabled is a harmles nop). The issue
with the GNU echo.c is really just that they do their own option parsing
(ie, they don't use getopt) and don't follow the usual
convention of using -- to denote the end of the option list.
Eg,
[rw@doppelsaurus]~#printf -n
bash: printf: -n: invalid option
printf: usage: printf [-v var] format [arguments]
but
[rw@doppelsaurus]~#printf -- -n
-n[rw@doppelsaurus]~#
Rainer Weikusat <rweikusat@talktalk.net> writes:
The issue with the GNU echo.c is really just that they do their own
option parsing (ie, they don't use getopt) and don't follow the usual
convention of using -- to denote the end of the option list.
Yes, most likely because of POSIX? Or should I say ``IEEE Std 1003.1-2017''?
The echo utility shall not recognize the "--" argument in the manner specified by Guideline 10 of XBD Utility Syntax Guidelines; "--" shall
be recognized as a string operand.
Implementations shall not support any options.
This is odd. I had seen the echo.c from some BSD system in the past. I don't recall which it was --- OpenBSD, FreeBSD, NetBSD. But I think it
was OpenBSD because OpenBSD's source still behaves I remember it.
Here's the behavior.
%echo -n -n hello
hello%
I remember this well because when I read it for the first time, there
was a loop looking for -n, which prompted me to ask --- omg, how am I
going to print ``-n'' to the terminal?
On 2021-11-24, Meredith Montgomery <mmontgomery@levado.to> wrote:
This is odd. I had seen the echo.c from some BSD system in the past. I
don't recall which it was --- OpenBSD, FreeBSD, NetBSD. But I think it
was OpenBSD because OpenBSD's source still behaves I remember it.
Here's the behavior.
%echo -n -n hello
hello%
According to POSIX, echo doesn't take any options, including the --
option to end options. See here:
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html
The echo utility shall not recognize the "--" argument in the manner
specified by Guideline 10 of XBD Utility Syntax Guidelines; "--" shall
be recognized as a string operand.
Implementations shall not support any options.
Thus, "echo -n -n hello" is required to print "-n -n hello".
echo -n is a GNU-ism that deviates from the spec.
Kaz Kylheku <480-992-1380@kylheku.com> writes:
On 2021-11-24, Meredith Montgomery <mmontgomery@levado.to> wrote:
This is odd. I had seen the echo.c from some BSD system in the past. I >>> don't recall which it was --- OpenBSD, FreeBSD, NetBSD. But I think it
was OpenBSD because OpenBSD's source still behaves I remember it.
Here's the behavior.
%echo -n -n hello
hello%
According to POSIX, echo doesn't take any options, including the --
option to end options. See here:
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html
The echo utility shall not recognize the "--" argument in the manner
specified by Guideline 10 of XBD Utility Syntax Guidelines; "--" shall
be recognized as a string operand.
Implementations shall not support any options.
Thus, "echo -n -n hello" is required to print "-n -n hello".
echo -n is a GNU-ism that deviates from the spec.
Do we know where the -n option originated? It exists in a number of different implementations of echo, not just GNU. My guess is that GNU
wasn't the origin.
Note that there are at least two GNU implementations of echo, the echo command provided by the coreutils package and the builtin echo command provided by GNU bash.
[...]
Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
Kaz Kylheku <480-992-1380@kylheku.com> writes:
On 2021-11-24, Meredith Montgomery <mmontgomery@levado.to> wrote:
This is odd. I had seen the echo.c from some BSD system in the past. I >>>> don't recall which it was --- OpenBSD, FreeBSD, NetBSD. But I think it >>>> was OpenBSD because OpenBSD's source still behaves I remember it.
Here's the behavior.
%echo -n -n hello
hello%
According to POSIX, echo doesn't take any options, including the --
option to end options. See here:
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html
The echo utility shall not recognize the "--" argument in the manner
specified by Guideline 10 of XBD Utility Syntax Guidelines; "--" shall >>> be recognized as a string operand.
Implementations shall not support any options.
Thus, "echo -n -n hello" is required to print "-n -n hello".
echo -n is a GNU-ism that deviates from the spec.
Do we know where the -n option originated? It exists in a number of
different implementations of echo, not just GNU. My guess is that GNU
wasn't the origin.
Note that there are at least two GNU implementations of echo, the echo
command provided by the coreutils package and the builtin echo command
provided by GNU bash.
[...]
I found some more information here: https://en.wikipedia.org/wiki/Echo_(command)#History
echo began within Multics. After it was programmed in C by Doug
McIlroy as a "finger exercise" and proved to be useful, it became
part of Version 2 Unix. echo -n in Version 7 replaced prompt, (which
behaved like echo but without terminating its output with a line
delimiter).
On PWB/UNIX and later Unix System III, echo started expanding C
escape sequences such as \n with the notable difference that octal
escape sequences were expressed as \0ooo instead of \ooo in C.
According to POSIX, echo doesn't take any options, including the --
option to end options. See here:
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html
The echo utility shall not recognize the "--" argument in the manner
specified by Guideline 10 of XBD Utility Syntax Guidelines; "--" shall
be recognized as a string operand.
Implementations shall not support any options.
Thus, "echo -n -n hello" is required to print "-n -n hello".
echo -n is a GNU-ism that deviates from the spec.
Note that there are at least two GNU implementations of echo, the echo command provided by the coreutils package and the builtin echo command provided by GNU bash.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 78:29:22 |
Calls: | 6,658 |
Calls today: | 4 |
Files: | 12,203 |
Messages: | 5,332,975 |
Posted today: | 1 |