Skip to content

Instantly share code, notes, and snippets.

@jesperronn
Last active October 10, 2025 04:56
Show Gist options
  • Select an option

  • Save jesperronn/c06371a3a2685af1a5eab71f543e09a5 to your computer and use it in GitHub Desktop.

Select an option

Save jesperronn/c06371a3a2685af1a5eab71f543e09a5 to your computer and use it in GitHub Desktop.

Revisions

  1. jesperronn revised this gist Oct 10, 2025. No changes.
  2. jesperronn revised this gist Oct 9, 2025. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions bash-template.sh
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,8 @@
    #
    # 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.github.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.
    #
  3. jesperronn created this gist Oct 9, 2025.
    95 changes: 95 additions & 0 deletions bash-template.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    #!/usr/bin/env bash
    #
    # this file is a template for bash scripts
    # it provides common functions and a standard structure for bash scripts
    #
    # 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