It is very important that Americans vote in the upcoming election. If you’ve voted, tag your post #IVoted!
MLTSHP
  • Popular
  • Join us! Sign up to post images and create your own shake.
    Sign Up!
  • sign in

bash errors

"Errors" a comic by Julia Evans @bork. 

Panel 1: by default, bash will continue after errors. Bash says "oh, was that an error? who cares, let's keep running!!!" Programmer with a blank face says "uh is that really the best choice?"

Panel 2: set -e stops the script on errors. set -e
unzip fle.zip <-- typo! script stops here
Programmer with a smiley face says "this makes your scripts WAY more predictable."

Panel 3: by default, unset variables don't error.
rm -r "$HOME/$SOMEPTH"
Bash says "$SOMEPTH doesn't exist? no problem, i'll just use an empty string!" Programmer with a frowny face says "OH NOOOO"

Panel 4: set -u stops the script on unset variables.
set -u
echo $UNSET <-- oops!
Bash says "I've never heard of that variable! STOP EVERYTHING!!!"

Panel 5: by default, a command failing doesn't fail the whole pipeline.
curl 404.com | grep 'panda'
Bash says "curl failed but grep succeeded so it's fine! success!"

Panel 6: set -o pipefail prevents this
Programmer with smiley face says "all of these are why I like to put set -euo pipefail at the beginning of all my scripts!
alt text
"Errors" a comic by Julia Evans @bork.

Panel 1: by default, bash will continue after errors. Bash says "oh, was that an error? who cares, let's keep running!!!" Programmer with a blank face says "uh is that really the best choice?"

Panel 2: set -e stops the script on errors. set -e
unzip fle.zip <-- typo! script stops here
Programmer with a smiley face says "this makes your scripts WAY more predictable."

Panel 3: by default, unset variables don't error.
rm -r "$HOME/$SOMEPTH"
Bash says "$SOMEPTH doesn't exist? no problem, i'll just use an empty string!" Programmer with a frowny face says "OH NOOOO"

Panel 4: set -u stops the script on unset variables.
set -u
echo $UNSET <-- oops!
Bash says "I've never heard of that variable! STOP EVERYTHING!!!"

Panel 5: by default, a command failing doesn't fail the whole pipeline.
curl 404.com | grep 'panda'
Bash says "curl failed but grep succeeded so it's fine! success!"

Panel 6: set -o pipefail prevents this
Programmer with smiley face says "all of these are why I like to put set -euo pipefail at the beginning of all my scripts!
source: https://wizardzines.com/comics...

tl;dr set -euo pipefail
1 year ago

Dan Phiffer pro

  • 1393 Views
  • 4 Saves
  • 41 Likes

Post URL

https://mltshp.com/p/1PBWN

In These Shakes

  • Dan Phiffer
  • Post to Facebook
  • Post to Tumblr
fishtaco 1 year ago
Thanks! Love these!
ang pro 1 year ago
If you must write a shell script, then defensive shell programming is a great idea.
mcmjolnir pro 1 year ago
also run em with `bash -x yerscript` to see where you fucked up besides unset vars and pipefails.
jxyzn pro 1 year ago
shellcheck is handy if not overly pedantic
bezt pro 1 year ago
Julia is great.
cristin pro 1 year ago
hit me with tcsh anyday.

Follow @best_of_mltshp on Mastodon

Are you a developer? Check out our API.

© MLTSHP, a Massachusetts Mutual Aid Society venture All Rights Reserved

  Terms of Use   Code of Conduct   Contact Us

Follow The MLTSHP User!