I've been using 'set -e' in my Bash scripts, which causes them to exit immediately when an error occurs. I get why this can be super helpful during development, but I'm curious about why it's not the default behavior in production scripts. Wouldn't it make more sense for scripts to stop when encountering an error, rather than running into issues later on due to dependencies?
2 Answers
Bash tries to maintain compatibility with the original Bourne shell, which doesn’t have 'set -e' enabled by default. If it did, it could potentially break many existing scripts that expect to continue running even if some commands fail. Plus, not all non-zero exit codes indicate an error—like 'grep' returning 1 when it finds no matches. Treating all non-zero exits as fatal would lead to a lot of messy workarounds in scripts.
There's a ton of complexities with 'set -e' that can make it tricky. You can look up specific issues, but generally speaking, it often makes poorly written scripts better while complicating well-structured ones.
I get what you're saying! But what’s the best way to handle errors similar to try/except in Python?
Great answer! It makes sense that backward compatibility is a priority.