No that is dangerous, consider this: set -e
myfunc() {
date %x # syntax error; returns 1, should be +%x
echo 'should not get here'
}
if var=$(myfunc); then
echo $var # erroneously prints 'should not get here'
else
echo failed
fi
Then you will ignore failure, which is bad.This is a variant of the issue that the sibling comment brought up -- error handling is disabled inside "if" conditions. In Oil the whole construct is unconditionally disabled by strict_errexit. It's too subtle. Oil has 2 ways of capturing the exit code, including run --assign :status -- mycommand # exit 0 but assign the status to a var
and shopt --unset errexit { # explicitly disable error handling if you want
mycmd
var status = $?
}
I'm looking for feedback to make sure that Oil has indeed fixed all of this stuff: https://www.oilshell.org/blog/2020/10/osh-features.htmlBasically the situation is "damned if you do and damned if you don't" in Bourne shell, so you need language/interpreter changes to really fix it. The rules are too tricky to remember even for shell experts -- there are persistent arguments on POSIX behavior that is over 20 years old, simply because it's so confusing. https://github.com/oilshell/oil/wiki/Where-To-Send-Feedback |
I don't know, I find it easier to not use set -e. I find it significantly easier to just explicitly handle all my errors. Having my script exit at some arbitrary point is almost never desirable.
I find chaining && and || pretty intuitive.
This is pretty contrived. I'd probably put the error handling in a function and then only handle the failure scenario: I never run into problems, this always works as expected, I don't need any language or interpreter changes to fix it. Once you realize `if` is just syntactic sugar and [ is just `test` then the world gets pretty simple.