A digression on echo. - Other *nix-like OSes & POSIX related

Users browsing this thread: 1 Guest(s)
seninha
Long time nixers
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.


Messages In This Thread
A digression on echo. - by seninha - 20-04-2020, 10:57 AM
RE: A digression on echo. - by jkl - 20-04-2020, 03:31 PM
RE: A digression on echo. - by z3bra - 20-04-2020, 03:40 PM
RE: A digression on echo. - by seninha - 20-04-2020, 04:29 PM
RE: A digression on echo. - by jkl - 20-04-2020, 04:33 PM
RE: A digression on echo. - by z3bra - 21-04-2020, 05:09 AM
RE: A digression on echo. - by jkl - 21-04-2020, 05:13 AM
RE: A digression on echo. - by twee - 21-04-2020, 05:22 PM
RE: A digression on echo. - by seninha - 28-11-2020, 11:23 AM