Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 27 Dec 2011 15:24:51 -0500
From:      Maxim Khitrov <max@mxcrypt.com>
To:        FreeBSD <freebsd-questions@freebsd.org>
Subject:   Unexpected sh behavior with EXIT trap and errexit
Message-ID:  <CAJcQMWcR9dUqv5fUifEk7r0qN55RJvTnoX2Os3NQqHaghtZ9QA@mail.gmail.com>

next in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAJcQMWcR9dUqv5fUifEk7r0qN55RJvTnoX2Os3NQqHaghtZ9QA>