A digression on echo. - Psychology, Philosophy, and Licenses
phillbush
After commenting on this thread I remembered a text on echo that Brian Kernighan and Rob Pike wrote in their book The UNIX Programming Environment called A digression on echo.

[Image: j5kXsXV.png]

The single option -n is not like other options in other utilities.
For example, while most utilities use getopt(3) to get the options, echo(1) should not use it, since a gotten option could be an argument to be echoed. See how OpenBSD implement echo, for example:

Code:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <err.h>

/* ARGSUSED */
int
main(int argc, char *argv[])
{
    int nflag;

    if (pledge("stdio", NULL) == -1)
        err(1, "pledge");

    /* This utility may NOT do getopt(3) option parsing. */
    if (*++argv && !strcmp(*argv, "-n")) {
        ++argv;
        nflag = 1;
    }
    else
        nflag = 0;

    while (*argv) {
        (void)fputs(*argv, stdout);
        if (*++argv)
            putchar(' ');
    }
    if (!nflag)
        putchar('\n');

    return 0;
}

Other than -n, modern echo implementations also add a new complexity: the C-like backslash sequences like '\n' and '\t'. Some echo implementations (such as OpenBSD's) just ignore them, but some (such as Bash built-in) don't. Here is another digression on echo about this very topic, by Doug Mcllroy, best known for proposing the UNIX pipelines and creating several UNIX tools:

[Image: gFPe9sR.png]

There is, however, another utility that does not print newlines when not asked for and that does interpret C-like backslash sequences: printf(1)

PS: I don't know the right forum to post it. I posted it here in the Philosophy forum since I think that the question on bloatedness and feature creep are related to UNIX Philosophy.
jkl
This seems to be one of the better examples on how Unix defaults to non-predictive behavior sometimes.
z3bra
Good job digging up the history behind it. it was a really interesting read !

Reading the source code for echo under different implementations has been an exercise many people have taken to compare code complexity (obviously, GNU "wins" here).

I personally only use "echo" to print out debug statements, or quickly output a variable name. For example, all my scripts feature this snippet:

Code:
usage() {
    echo "usage: $(basename $0) [-flags] args..." >&2
}

Whenever I need to print out formatted stuff, printf(1) is my goto as it is much more convenient.
phillbush
(20-04-2020, 03:40 PM)z3bra Wrote: Reading the source code for echo under different implementations has been an exercise many people have taken to compare code complexity (obviously, GNU "wins" here).

This comparison remembered me of this meme, based on longcat.

[Image: 7r5Q1qA.png]
jkl
I wonder how much of the GNU code is a try to circumvent the limitations imposed by its libc compared to Plan 9’s.
z3bra
Hahaha awesome meme !

(20-04-2020, 04:33 PM)jkl Wrote: I wonder how much of the GNU code is a try to circumvent the limitations imposed by its libc compared to Plan 9’s.

I think they're more trying to circumvent their own coding guidelines than the libc… Which is even more terrifying if you ask me. I mean. what the hell is this? or that? I'm speechless…
jkl
"GNU's Not Unix. And here's how we mean that."
twee
Reading the GNU coding standards was what convinced me to move back to OpenBSD, after having had to move back to Linux for certain software support which is no longer an issue. In particular, this paragraph:

Quote: For instance, Standard C says that nearly all extensions to C are
prohibited. How silly! GCC implements many extensions, some of which
were later adopted as part of the standard. If you want these
constructs to give an error message as "required" by the standard, you
must specify '--pedantic', which was implemented only so that we can say
"GCC is a 100% implementation of the standard", not because there is any
reason to actually use it.

It reads to me both as quite immature and as something of a "Microsoftian" attitude. Every time I see GNU code I find it offputting, and I wouldn't even really consider myself a UNIX purist.




Members  |  Stats  |  Night Mode