Skip to content

Instantly share code, notes, and snippets.

@Kerruba
Forked from stepf/bash-try-catch.sh
Created March 15, 2017 12:07
Show Gist options
  • Save Kerruba/4d2e4c3d584c45b962d949ed7a7e47e9 to your computer and use it in GitHub Desktop.
Save Kerruba/4d2e4c3d584c45b962d949ed7a7e47e9 to your computer and use it in GitHub Desktop.
Try-Catch Behavior in Bash
#!/bin/bash --
# Note -- will make script continue despite errors
echo "Some command that fails"
RETURN_CODE=$?; #capture error code here (if any)
echo "Cleanup before script is allowed to fail"
[[ $RETURN_CODE -ne 0 ]] && exit $RETURN_CODE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment