Last active
October 10, 2025 04:56
-
-
Save jesperronn/c06371a3a2685af1a5eab71f543e09a5 to your computer and use it in GitHub Desktop.
template for bash scripts
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 | |
# | |
# this file is a template for bash scripts | |
# it provides common functions and a standard structure for bash scripts | |
# | |
# curl -L --silent --remote-name https://gist.githubusercontent.com/jesperronn/c06371a3a2685af1a5eab71f543e09a5/raw/bash-template.sh && chmod +x bash-template.sh | |
# | |
# Agents can use this template to create new bash scripts quickly and easily. | |
# | |
# Format, there are 3 main sections: | |
# section #1. Header: shebang, set -euo pipefail, pushd to script dir, fixed variables | |
# section #2. Functions: usage, parse_args, check_program, check_prereqs | |
# section #3. Specific functions | |
# section #4. Main: run_main function | |
# start section #1 - euo pipefail, pushd, fixed variables | |
set -euo pipefail | |
pushd "$(dirname "$0")/.." > /dev/null || exit 1 | |
# fixed variables go here | |
# color variables | |
C_0="\e[0m" | |
C_BOLD="\e[1m" | |
C_RED="\e[31m" | |
C_GREEN="\e[32m" | |
C_ORANGE="\e[38;5;202m" | |
C_CYAN="\e[36m" | |
C_DIM="\e[37m" | |
# other variables | |
# (empty in template) | |
# start section #2. Functions: usage, parse_args, check_program, check_prereqs | |
function usage() { | |
echo "Usage: $0 [options]" | |
echo "Options:" | |
echo " -h, --help Show this help message and exit" | |
} | |
function parse_args() { | |
while [[ $# -gt 0 ]] | |
do | |
case "$1" in | |
-h|--help) | |
usage | |
exit 0 | |
;; | |
*) | |
echo "Unknown option: $1" | |
usage | |
exit 1 | |
;; | |
esac | |
shift | |
done | |
} | |
check_program() { | |
set +e | |
command_to_test=$(command -v "${1}") | |
command_exit_code=$? | |
set -e | |
if [[ "${command_exit_code}" -ne 0 ]]; then | |
echo -e "${C_BOLD}${C_RED}FATAL: Required ${1} command not found. You MUST install ${1} manually${C_0}" >&2 | |
return 9 | |
else | |
echo -e "✅ ${C_GREEN}Found ${1} ${C_DIM}[${command_to_test}]${C_0}" | |
fi | |
} | |
check_prereqs() { | |
if [[ "${RUN_ALL:-}" != "true" ]]; then | |
return | |
fi | |
echo -e "${C_CYAN}Check prereqs... Testing for required commands availability${C_0}" | |
check_program bash | |
echo -e "${C_CYAN}Check prereqs... All relevant commands available✅${C_0}" | |
} | |
# start section #3. Specific functions | |
# (deliberately empty in the template) | |
# start section #4. Main: run_main function. This is a separate function to make file testable | |
run_main() { | |
parse_args "$@" | |
check_prereqs | |
# here you add the main logic of your script | |
echo "TODO: call the main program here" | |
} | |
if [[ "${BASH_SOURCE[0]}" == "${0}" ]] | |
then | |
run_main "$@" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment