Consider this snippet:
stop () {
echo "${1}" 1>&2
exit 1
}
func () {
if false; then
echo "foo"
else
stop "something went wrong"
fi
}
Normally when func
is called it will cause the script to terminate, which is the intended behaviour. However, if it's executed in a sub-shell, such as in
result=`func`
it will not exit the script. This means the calling code has to check the exit status of the function every time. Is there a way to avoid this? Is this what set -e
is for?
func
. – Sep 18 '12 at 16:42func
is being called from within an evaluation likeecho "hello $(func)"
, then this is the only working solution I have found: https://stackoverflow.com/a/9894126/1117929 – Jonathan Cross Mar 14 '22 at 15:48