planet cb

errors:
by default, bash will continue after errors.
Bash: oh, was that an error? who care, let's keep running!
Programmer: Uh that is NOT what I wanted.
set -e stops the script on errors.
this makes your script WAY more predictable.
by default, unset variables don't error.
rm -rf "$HOME/$SOMEPTH".
bash: $SOMEPTH doesn't exist? no problem, i'll just use an empty string!
programmer: OH NOOO that means rm -rf $HOME.
set -u stops the script on unset variables.
bash: I've never heard of $SOMEPTH! STOP EVERYTHING!!!
by default, a command failing doesn't fail the whole pipeline.
curl yxqzq.ca | grep 'panda'
bash: curl failed but grep succeeded so it's fine! success!
set -o pipefail makes the pipe fail if any command fails.
you can combine set -e, set -u and set -o pipefail into one command I put at the top of all my scripts:
set -euo pipefail

@tamtararam -eou is more memorable for me because it sounds like "eww don't do that".

@polychrome eou pipefail.
yesterday i learned that if you forget the pipefail, it just prints out all options on your system.

@tamtararam It's so easy to remember these. It's `set -e` for "Exit on Error" and `set -x` for "eXo". You just have to remember that it's Greek.

@freakazoid @tamtararam set -x is GREAT for debugging! It will just print every command it runs. Very helpful!

@rgegriff @tamtararam And now that I have finally figured out a mnemonic I'll be able to use `set -e` by itself without having to look up in the man page which is which.

@freakazoid @tamtararam I always remembered e for error, x for x-tremely verbose

@tamtararam Oh, I had no idea one can screw themselves so badly with Bash...

@tamtararam @unfa this is awesome, thanks for sharing!
replies
0
announces
0
likes
1