Skip to content

Instantly share code, notes, and snippets.

@adrienjoly
Last active January 10, 2022 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrienjoly/c096de6e8339d90b20269aa6d2c6bed2 to your computer and use it in GitHub Desktop.
Save adrienjoly/c096de6e8339d90b20269aa6d2c6bed2 to your computer and use it in GitHub Desktop.
Clean up with trap
#!/bin/bash
set -e # will stop the script if any command fails with a non-zero exit code
function cleanup {
./teardown.sh || true # keep tearing down, even if file does not exist
echo "🧹 Cleaned up."
}
trap cleanup EXIT
./do-whatever.sh
# Even if do-whatever.sh fails, trap will run the cleanup function.
# In case of error, the script will return a non-zero exit code, as usual.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment