Skip to content

Instantly share code, notes, and snippets.

@ucyo
Last active February 6, 2026 15:33
Show Gist options
  • Select an option

  • Save ucyo/1eadce81192ee0a5b3cfccd500707503 to your computer and use it in GitHub Desktop.

Select an option

Save ucyo/1eadce81192ee0a5b3cfccd500707503 to your computer and use it in GitHub Desktop.
Sane bash script template
#!/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