Bash

Safe Bash Script Template

admin by @admin ADMIN
2d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Start every script with the same skeleton: strict error mode (errexit, nounset, pipefail), an IFS reset, and a main() function. Catches silent failures that have lost engineers weeks of debugging.
Bash
Raw
#!/usr/bin/env bash
set -euo pipefail        # exit on error, on unset var, on pipe failure
IFS=$'\n\t'              # safer field splitting (no spaces)

# Optional: better stack traces on error
trap 'echo "Error on line $LINENO" >&2' ERR

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

main() {
    local arg="${1:-default}"
    echo "Hello from $SCRIPT_DIR with arg=$arg"
}

main "$@"
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.