Skip to content

Instantly share code, notes, and snippets.

@philpennock
Created May 19, 2015 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philpennock/1babd79316413579a343 to your computer and use it in GitHub Desktop.
Save philpennock/1babd79316413579a343 to your computer and use it in GitHub Desktop.
Shell sucks; tried in bash, zsh, BSD sh
#!/bin/bash
script_name="$(basename "$0")"
die() {
local rv=$?
printf >&2 "%s: %s\n" "$script_name" "$*"
exit $rv
}
will_fail_return() {
echo "in will_fail_return"
return 42
}
(
set -e -u
set +o
will_fail_return
echo $?
echo "got to end of subshell"
) || die "subshell failed"
echo "reached end of entire script"
@philpennock
Copy link
Author

Of course. The problem is that -e must be somewhere defined to exit at the end of a scope of execution, which includes the entire subshell. So -e will never exit early inside a sub-shell. Moving the set -e -u above the sub-shell doesn't change things.
GRRRARGH.

@philpennock
Copy link
Author

It's not that. It's because of the || die at the end of the sub-shell, which changes how -e is applied within the subshell. Without || die, -e will cause an early exit. But with -e not in effect within the subshell, this is ... horrible.

So, need to do explicit die afterwards.

Oh fscking eww.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment