5 ms·
I can recommend starting every bash script with set -euxo pipefail The "u" has basically the same effect as the question mark, but for every variable usag
by matrss 2y ago
I can recommend starting every bash script with
set -euxo pipefail
The "u" has basically the same effect as the question mark, but for every variable usage.
- ko1nksm 2y agoset -o pipefail is now POSIX compliant. Use it in all POSIX shell scripts, not just bash scripts.
- matrss 2y agoOh wow, I didn't know that. I am pretty sure I had a sh trip up on it just recently, so I thought it still was bash only. Yes, use it everywhere possible.
- yjftsjthsd-h 2y agoEmphasis on now - it's new in this version, so probably expect a little bit of delay before it's actually available everywhere. But yes, excellent addition and I'm happy to have it available more broadly now.
- cesarb 2y agoI do the same, except for the "x" (that is, "set -euo pipefail"); depending on what you're doing, "set -x" might be helpful, might be too much noise, or it could even break things which were not expecting the extra output (and in the worst case, it might end up echoing secret tokens into your build logs).
- matrss 2y agoI find it to be a good default when writing a script, but yes, it can get noisy and potentially leak stuff, if that is what your script deals with. That hasn't been a concern in the settings I usually use it, though. I am not sure how it could break anything though, unless you are parsing stderr of your script in a subsequent step, which would seem unusual anyway.
- tommiegannert 2y agoOh, it seems set -u was fixed with regards to arrays in 2011 or so: https://stackoverflow.com/questions/7577052/unbound-variable-error-in-bash-when-expanding-empty-array https://stackoverflow.com/questions/7577052/unbound-variable... Maybe I can start using it again. (I think I noticed that issue while I was at Google, and they used an older version of Bash.)
- Maledictus 2y agoI prefer set -Eeuxo pipefail, so my ERR trap is inherited in functions.