[TUHS] POSIX ed(1)'s exit status. (Was: diving into vi...)

Ralph Corderoy ralph at inputplus.co.uk
Sat Jun 8 20:19:12 AEST 2024


Hi,

Will Senn wrote:
> I use ed(1) a lot too for quick edits.

Me too.  I've heard others who have told crontab(1) or their mail
program to use ed have been bitten by the exit status varying between
0 and 1.  ed(1p) explains:

    EXIT STATUS
        The following exit values shall be returned:

         0    Successful completion without any file or command errors.
        >0    An error occurred.

This behaviour is surprising.  Here's GNU ed:

    $ ed /tmp/foo
        /tmp/foo: No such file or directory
        a
        foo
        .
        wq
        4
    $ echo $?
        0
    $ ed /tmp/foo
        4
        /bar
        ?
        $a
        bar
        .
        wq
        8
    $ echo $?
        1
    $

I assume POSIX made it the default behaviour to be useful when ed isn't
talking to mankind.  Perhaps they think that's the default these days.
GNU ed added -l:

       -l, --loose-exit-status
              exit with 0 status even if a command fails

>From https://man.netbsd.org/ed.1, I don't think BSD ed has a similar
option.  Probably, because it doesn't need it as my quick skim of
http://bxr.su/NetBSD/bin/ed/main.c#220 suggests it will exit(0) even if
an earlier search found nothing.  There is a list of BSD's differences
to POSIX, e.g. ‘z’ for scrolling, amidst the source,
http://bxr.su/NetBSD/bin/ed/POSIX, but it doesn't mention the exit
status.

-- 
Cheers, Ralph.


More information about the TUHS mailing list