Created
December 30, 2025 15:09
-
-
Save brandonhimpfen/76fc1a3613fb799b942a3ac2df802f06 to your computer and use it in GitHub Desktop.
Bash strict mode starter template using set -Eeuo pipefail, with safe defaults, error handling, and argument parsing.
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 | |
| # ----------------------------------------------------------------------------- | |
| # Bash Strict Mode Starter Template | |
| # ----------------------------------------------------------------------------- | |
| # -e : Exit immediately if a command exits with a non-zero status | |
| # -u : Treat unset variables as an error | |
| # -o pipefail : Fail a pipeline if any command fails | |
| # -E : ERR traps are inherited by shell functions | |
| # ----------------------------------------------------------------------------- | |
| set -Eeuo pipefail | |
| # ----------------------------------------------------------------------------- | |
| # Globals | |
| # ----------------------------------------------------------------------------- | |
| SCRIPT_NAME="$(basename "$0")" | |
| # ----------------------------------------------------------------------------- | |
| # Logging helpers | |
| # ----------------------------------------------------------------------------- | |
| log() { | |
| printf '[%s] %s\n' "$SCRIPT_NAME" "$*" | |
| } | |
| error() { | |
| printf '[%s] ERROR: %s\n' "$SCRIPT_NAME" "$*" >&2 | |
| } | |
| # ----------------------------------------------------------------------------- | |
| # Error handler | |
| # ----------------------------------------------------------------------------- | |
| trap 'error "Command failed at line $LINENO"; exit 1' ERR | |
| # ----------------------------------------------------------------------------- | |
| # Usage | |
| # ----------------------------------------------------------------------------- | |
| usage() { | |
| cat <<EOF | |
| Usage: $SCRIPT_NAME [options] | |
| Options: | |
| -h, --help Show this help message | |
| EOF | |
| } | |
| # ----------------------------------------------------------------------------- | |
| # Argument parsing | |
| # ----------------------------------------------------------------------------- | |
| while [[ $# -gt 0 ]]; do | |
| case "$1" in | |
| -h|--help) | |
| usage | |
| exit 0 | |
| ;; | |
| *) | |
| error "Unknown option: $1" | |
| usage | |
| exit 1 | |
| ;; | |
| esac | |
| done | |
| # ----------------------------------------------------------------------------- | |
| # Main logic | |
| # ----------------------------------------------------------------------------- | |
| main() { | |
| log "Script started" | |
| # Example command | |
| # ls /nonexistent # Uncomment to see strict mode in action | |
| log "Script finished successfully" | |
| } | |
| main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment