bash-script-writing

📁 steveclarke/dotfiles 📅 Jan 25, 2026
12
总安装量
2
周安装量
#25839
全站排名
安装命令
npx skills add https://github.com/steveclarke/dotfiles --skill bash-script-writing

Agent 安装分布

claude-code 2
windsurf 1
opencode 1
cursor 1
codex 1
antigravity 1

Skill 文档

Modular Bash Script Writing

Write bash scripts with clear structure, proper error handling, and modular functions.

Core Principles

  • Strict mode: Start with set -euo pipefail
  • Functions over monoliths: One function per logical operation
  • Document at top: Header with purpose, usage, configuration
  • Fail fast with clarity: Descriptive errors with fix suggestions

Script Structure

Place main() immediately after variables so workflow is visible at file top.

#!/usr/bin/env bash
set -euo pipefail

################################################################################
# Script Title
################################################################################
#
# OVERVIEW
# --------
# Brief description of what this script does
#
# USAGE
# -----
#   script-name arg1 arg2
#
# CONFIGURATION
# -------------
# Environment variables or config requirements
#
################################################################################

# Colors
green='\033[0;32m'
blue='\033[0;34m'
yellow='\033[1;33m'
red='\033[0;31m'
nc='\033[0m'

# Configuration
VAR_ONE=${VAR_ONE:-default}
VAR_TWO=${VAR_TWO:?VAR_TWO is required}

################################################################################
# Main Orchestration
################################################################################

main() {
  parse_arguments "$@"
  step_one
  step_two
  log "Complete!"
}

################################################################################
# Helper Functions
################################################################################

log() { echo -e "${green}==>${nc} ${1}"; }
info() { echo -e "${blue}Info:${nc} ${1}"; }
warn() { echo -e "${yellow}Warning:${nc} ${1}"; }
error() { echo -e "${red}Error:${nc} ${1}" >&2; exit 1; }

################################################################################
# Core Functions
################################################################################

function_name() {
  log "What this function does..."
  # Implementation
  log "Complete"
}

################################################################################
# Script Execution
################################################################################

main "$@"

Function Guidelines

  • Name clearly: Verb-based names (setup_directory, validate_config)
  • One purpose: If you need “and” to describe it, split it
  • Log progress: Start with log "...", end with status
  • Return codes: return 0 success, return 1 failure
  • Keep focused: 5-20 lines per function

Error Handling

# Good: Clear error with context
setup_directory() {
  log "Creating deployment directory..."
  if ! ssh "$HOST" "mkdir -p /app/deploy"; then
    error "Failed to create directory on $HOST"
  fi
  log "Directory ready"
}

# Bad: Silent failure
setup_directory() {
  ssh "$HOST" "mkdir -p /app/deploy"
}

Quality Checklist

  • Strict mode (set -euo pipefail)
  • Header with overview and usage
  • Helper functions (log, info, warn, error)
  • Core logic in named functions
  • Main() shows clear flow at top
  • Error messages include context
  • No silent failures
  • Meaningful variable names
  • 2-space indentation
  • Functions grouped with section headers