Skip to content

Instantly share code, notes, and snippets.

@frontrangerider2004
Created June 29, 2022 15:35
Show Gist options
  • Save frontrangerider2004/7c3f6bd90881c1d5eb44bfe16bb732b6 to your computer and use it in GitHub Desktop.
Save frontrangerider2004/7c3f6bd90881c1d5eb44bfe16bb732b6 to your computer and use it in GitHub Desktop.
Bash Exit Traps
# optional exit code
CODE=0
cleanup() {
echo "[${SCRIPT_NAME}] Cleaning stuff up..."
exit ${CODE}
}
# should be defined early in the script since bash doesn't provide function hoisting
trap cleanup EXIT
# The meat of the script goes here
# you can use $? to get the return code of any cmd and set $CODE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment