4 ms·
Is it better to set -e or check return code of processes and decide to exit?
by minroot 3y ago
Is it better to set -e or check return code of processes and decide to exit?
- zoogeny 3y agoBetter/worse is one of those subjective things that comes with experience. No one can definitively answer such a question in a way that will apply to every circumstance. But as someone who rarely writes bash scripts but just enough to be dangerous ... I use `set -xe` at the top of every single shell script I write. The -x flag causes the script to echo every command that is executed to stdout which can be very valuable if I am debugging a shell script that is running on an external environment (like a CI). I've seen this habit suggested many times and it has served me well.
- OJFord 3y agoMine's `set -eEuo pipefail`. I'd have to look up exactly what's what, but if you're inclined to `-e` you probably want the others too. (Off the top of my head I think E is the same thing in functions, u is bail out if a variable is used without being defined (instead of treating it as empty string), and pipefail is similar for when you pipe to something else like `this_errs | grep something`.) I don't like `-x` personally, I find it way too verbose; more confusing than helpful.
- imp0cat 3y agoYeah, `-x` can be annoying if it's always on. My suggestion would be to run your script like this `bash -x your_script.sh` when (and only when) you need to debug something.