User avatar
Saneef H. Ansari @saneef@front-end.social
1mo
TIL, the importance of having set -e inside a Bash script, the hard way.

For those who are curious,
set -e tells a Bash script to exit if any commands return a non-zero exit status (failure). It is important to have this in pre-commit or pre-deploy scripts. Otherwise, you are relying only on the return value of the last command in the script. If any previous command fails, the script returns a success status.
2
0
1
0
User avatar
Xenotime @xeno@snug.moe
1mo
@saneef
Remember to always set -euo pipefail or else bash will do everything it can to destroy your computer.

-Kevin Fang Video
2
0
0
0
1mo
@xeno @saneef this is actually bad advice, because at very least set -e is full of pitfalls and its behavior apparently changes between versions, making bash more likely to destroy your computer, see mywiki.wooledge.org/BashFAQ/105

If you need to exit your script if a command fails, add
|| exit (or || return in a function) at the end:
meow || exit
nyaa || exit
mrrp || exit




exit and return without an argument abort execution returning the exit code of the previous command, which in an OR chain would be the left side of the operator (you can also write it as e.g. exit 1 to set the error code explicitly). alternatively you can chain all your commands with AND between them, which may be more convenient:
meow &&
nyaa &&
mrrp # perhaps add an `|| exit` here




either way, you make your error handling explicit, which is good. one could say that it’s way too easy to mess this up and miss a line, but if you’re writing shell scripts you should generally be prepared for your journey to be a minefield and take great care with every step. If that is impractical for you it may be better to use a language better suited for writing robust programs.
2
1
1
0
User avatar
illy [Shrimple-mode] protomoji_orange_flag_lesbian @illyBytes@shrimp.imsofucking.gay
1mo
@kimapr @xeno @saneef can related pipefail fucked me before 😭
0
0
1
0