Hacker News new | past | comments | ask | show | jobs | submit login

Is it better to set -e or check return code of processes and decide to exit?



Better/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.


Mine'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.


Yeah, `-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.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: