Re: set -e again
"Greg A. Woods" <[email protected]> Tue, 13 Jan 2026 18:14:51 -0800
| Newsgroups | gmane.os.netbsd.devel.userlevel |
|---|---|
| Organization | Planix, Inc. |
| Message-ID | <[email protected]> |
--pgp-sign-Multipart_Tue_Jan_13_18:14:43_2026-1 Content-Type: text/plain; charset=US-ASCII At Wed, 14 Jan 2026 01:18:47 +0000, David Holland <[email protected]> wrote: Subject: set -e again > > I discovered the following today. > > This: > set -e > f() { > echo foo > false > echo bar > } > f > > as one might expect, prints "foo" and exits. > > However, this: > set -e > f() { > echo foo > false > echo bar > } > f || echo baz > echo buzz > > prints "foo", "bar", "buzz", and continues. Furthermore, all the > shells I have in easy reach agree on it. That makes perfect sense to me. Note that if the function invocation is not "guarded" by testing its exit status then execution is terminated after the false statement just as one would also expect. Shell functions are effectively macros. Replace the invocation of f() with its "value" and it now looks like: $ set -e $ { echo foo; false; echo bar; } || echo baz; echo buzz foo bar buzz $ echo $? 0 Perhaps another way to think about it is that the part wrapped in brackets is a compound command, and indeed the manual concurs. A function definition is most simply "f() command", and of course the "command" part can be either a simple command or a list (i.e. a form of compound command). So since the compound command is followed by an operator that tests its status, the whole compound command is executed in the guarded environment. The manual also says: "The function completes after having executed _command_ with [[its]] exit status set to the status returned by _command_." though obviously that statement doesn't take into account an un-guarded execution of a function in a shell with errexit("set -e") set. > Is this intended, and is there a way to get the user's intended > behavior of exit on unchecked failure back? (E.g. is there a different > set to get functions to return on unchecked failure that might cover > this? I would definitely say it is/was intended. Perhaps as an extension the shell could override the guard if a function definition includes a call to "set -e" within it. -- Greg A. Woods <[email protected]> Kelowna, BC +1 250 762-7675 RoboHack <[email protected]> Planix, Inc. <[email protected]> Avoncote Farms <[email protected]> --pgp-sign-Multipart_Tue_Jan_13_18:14:43_2026-1 Content-Type: application/pgp-signature Content-Transfer-Encoding: 7bit Content-Description: OpenPGP Digital Signature -----BEGIN PGP SIGNATURE----- iF0EABECAB0WIQRuK6dmwVAucmRxuh9mfXG3eL/0fwUCaWb8FgAKCRBmfXG3eL/0 f43iAJ9VFJGwPtIkrX66ChYl5YI8tCTangCZAayvNaojaXc42n/yLbmPl0ibt9c= =usPj -----END PGP SIGNATURE----- --pgp-sign-Multipart_Tue_Jan_13_18:14:43_2026-1--