Unexpected sh behavior with EXIT trap and errexit

Maxim Khitrov max at mxcrypt.com
Tue Dec 27 20:25:23 UTC 2011


Can anyone explain this behavior (FreeBSD 9.0-RC3 amd64):

Script:
----
#!/bin/sh

cleanup()
{
    echo 'first'
    echo 'second'
}

fail() { return 42; }

trap cleanup EXIT
set -o errexit
fail
----

Output:
----
first
----

Now comment out 'set -o errexit', replace 'fail' with 'fail || exit'
(which should be equivalent to using errexit), and run again.

Output:
----
first
second
----

Am I doing something stupid or is this a bug? Bash prints out the same
(second) output for both versions of the code.

- Max


More information about the freebsd-questions mailing list