Last active
February 6, 2026 15:33
-
-
Save ucyo/1eadce81192ee0a5b3cfccd500707503 to your computer and use it in GitHub Desktop.
Sane bash script template
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
| #!/usr/bin/env bash | |
| set -Ee # fail script, if single command fails | |
| set -u # unset variables are treated as errors | |
| set -f # no filename extension | |
| # set -x # verbose mode | |
| set -o pipefail # if a subcommand from a pipe fails, the whole pipe fails | |
| trap cleanup SIGINT SIGTERM ERR EXIT # execute cleanup function at following signals | |
| SOURCE_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P) # location of script | |
| WORKING_DIR=$(pwd -P) # working directory | |
| cleanup() { | |
| trap - SIGINT SIGTERM ERR EXIT | |
| # script cleanup here | |
| echo "🧹 Time for cleanup" | |
| } | |
| echo "😊 This script is located at '${SOURCE_DIR}'." | |
| echo "😊 This script was exectued from '${WORKING_DIR}'." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment