On Tue, Sep 01, 2026 at 08:07:15AM +0700, Robert Elz wrote:
> Date: Mon, 31 Aug 2026 09:53:50 -0400
> From: Chet Ramey <[email protected]>
> Message-ID: <[email protected]>
>
> Nb: there is a difference between a command that ends
> up being empty, and one which never contained anything at all - the former
> is defined as having a 0 exit status, so in
>
> E=
> $E || echo failed
>
> "failed" can never be printed.
Useful to know that this works.
Seems the E= is not needed (just to clear it), per sé (at least in bash):
$ unset E
$ $E
$ $E || echo failed
all work.
> All this, including that many (most) people don't just know it all,
> just gives even more reasons why using -e (usually) is idiotic.
Yes, 'set -e' is hard to use properly.
Is this description correct?
If the shell used the exit status, then set -e is not triggered.
> The best thing to do in any script, is to write the whole script inside a
> function (often called "main", its name is of no consequence, provided it
> is new to the script, but that is what I will assume here), and then after
> the definition of:
>
> main() {
> # the whole script goes here (no need to indent it)
> }
Good advice.
This is almost what I have learned to do.
> just add
> main "$@" && exit || exit # end of script
But not (yet) doing this.
Isn't this enough at the end of the script?:
main "$@" || exit
> as the last line of the file (though it is harmless to have anything else
> appropriate after it). Nothing other than whitespace or comments between
> the '}' that ends the main() function definition, and this line.
I have a script structure of:
## description
global vars
main() { ...; }
functions called from main
main "$" || exit ## since recent ;)
I like that you don't have to worry about functions that are not yet defined.
The only problem might be that the whole script needs to be read in from disk.
> should there be a need, the file containing the script can be modified (with
> added, or deleted, sections) without affecting in any way any current
> instances
> of the script that might be still running.
O yes, been bitten by that.
Is this still a problem in current shells/bash?
I see (via strace) that bash reads the whole 100K of a test script in one go.
Which fits nicely with the script structure (as the read is done anyway).
--
Regards, Mike Jonkmans