The Script Template (script-template.sh) provides a standardized starting point for creating new bash scripts. It includes pre-configured color codes, logging functions, argument parsing, and follows bash best practices with proper error handling.
Use this template as the foundation for all new bash scripts to ensure consistency, maintainability, and robust error handling across your script collection.
Return the exit code of the first failed command in a pipeline
set -eo pipefail
Why This Matters
Without these flags:
# Without -e: script continues even after failurerm important-file.txtecho "File deleted" # Runs even if rm failed# Without pipefail: only last command's exit code is checkedcat missing.txt | grep error # Succeeds if grep works, ignoring cat failure
With these flags:
# With -e: script exits on first failurerm important-file.txt # If this fails, script stopsecho "File deleted" # Never executed# With pipefail: entire pipeline fails if any command failscat missing.txt | grep error # Fails if cat fails, even if grep would succeed
RED='\033[0;31m'GREEN='\033[0;32m'YELLOW='\033[1;33m'BLUE='\033[0;34m'MAGENTA='\033[0;35m'CYAN='\033[0;36m'BOLD='\033[1m'DIM='\033[2m'NC='\033[0m' # No Color
show_help() { cat << EOF${BOLD}${CYAN}script-name.sh${NC} - Brief description${BOLD}USAGE:${NC} ./script-name.sh [OPTIONS] [ARGS]${BOLD}OPTIONS:${NC} ${GREEN}-h, --help${NC} Show this help message ${GREEN}-d, --debug${NC} Enable debug mode (dry-run) ${GREEN}-v, --verbose${NC} Verbose output${BOLD}EXAMPLES:${NC} ${DIM}# Basic usage${NC} ./script-name.sh ${DIM}# Debug mode${NC} ./script-name.sh --debug${BOLD}DESCRIPTION:${NC} Add a longer description here explaining what your script does, its purpose, and any important details users should know.EOF exit 0}
Customize the help output by editing the show_help() function. Add sections for requirements, configuration files, examples, or troubleshooting.
#!/bin/bash# my-backup.sh - Backs up important directoriesset -eo pipefail# Load template helpers...main() { log "Starting backup..." if [[ $DEBUG -eq 1 ]]; then debug "Debug mode enabled" warning "This is a dry-run, no changes will be made" # Show what would happen else # Do actual backup tar -czf backup.tar.gz ~/documents success "Backup created: backup.tar.gz" fi}main "$@"
#!/bin/bash# process-file.sh - Processes a data fileset -eo pipefail# Colors and helpers...FILE=""# Add file argumentwhile [[ $# -gt 0 ]]; do case $1 in -f|--file) FILE="$2" shift 2 ;; # ... other arguments esacdonemain() { if [[ -z "$FILE" ]]; then error "File is required" echo "Use: $0 --file input.txt" exit 1 fi if [[ ! -f "$FILE" ]]; then error "File not found: $FILE" exit 1 fi log "Processing file: $FILE" # Process the file... success "File processed successfully"}main
check_dependencies() { local deps=("curl" "jq" "git") local missing=() for dep in "${deps[@]}"; do if ! command -v "$dep" &> /dev/null; then missing+=("$dep") fi done if [[ ${#missing[@]} -gt 0 ]]; then error "Missing dependencies: ${missing[*]}" echo "Install with: apt install ${missing[*]}" exit 1 fi}main() { check_dependencies # Rest of script...}
# Test argument parsing./my-script.sh --help./my-script.sh --debug# Test error handling./my-script.sh --invalid-flag# Test with ShellCheckshellcheck my-script.sh