Created
June 29, 2022 15:35
-
-
Save frontrangerider2004/7c3f6bd90881c1d5eb44bfe16bb732b6 to your computer and use it in GitHub Desktop.
Bash Exit Traps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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