Re: set -e again
RVP <[email protected]> Wed, 14 Jan 2026 13:39:42 +0000 (UTC)
| Newsgroups | gmane.os.netbsd.devel.userlevel |
|---|---|
| Message-ID | <[email protected]> |
On Wed, 14 Jan 2026, David Holland wrote:
> 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.
>
> This seems wrong - the exit status of the f is guarded, but the false
> is not. But also, the fact that everybody agrees makes me think it's
> probably the agreed result of the last round of POSIX wrangling over
> the -e definition some years back.
>
> 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?)
>
There's a way, but, it a) involves running functions in a subshell, and b)
set +e/-e fiddling before/after each function:
```
set -e # orig.
f() {
set -e # for func.
echo foo
false
echo bar
}
set +e # for every func.
(f); rc=$?
set -e
[ $rc -eq 0 ] || echo baz
echo buzz
```
-RVP