Bash Pro
Seth HobsonCommunityCreated by Ryan SnodgrassDistributed by Seth Hobson
- Subagent
- Guidelines
- Software development
- MIT
- No issues found
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
bash-pro provides a rigorous framework for developing production-grade Bash scripts. It emphasizes defensive programming, strict error handling, and cross-platform portability. The skill covers the entire development lifecycle, including static analysis, testing, CI/CD integration, and security hardening, ensuring that shell scripts are reliable enough for critical automation and infrastructure tasks.
What it does
- Establishes defensive coding patterns like strict error handling and proper variable quoting.
- Provides best practices for POSIX compliance and cross-platform portability.
- Integrates static analysis and formatting tools to maintain code quality.
- Defines structured approaches for logging, observability, and error reporting.
- Outlines security hardening techniques to prevent common shell vulnerabilities.
- Offers guidance on testing frameworks and CI/CD pipeline automation.
When to use it
- When building complex automation scripts for CI/CD pipelines.
- When developing system utilities that require high reliability and safety.
- When refactoring legacy shell scripts to modern Bash 5.x standards.
- When establishing team-wide standards for shell script development.
- When preparing scripts for production deployment in diverse environments.
Advantages
- Reduces runtime errors through strict error propagation and input validation.
- Improves script maintainability and readability through consistent style.
- Enhances security by mitigating injection risks and credential leaks.
- Ensures portability across different Linux and macOS environments.
- Facilitates automated testing and quality assurance for shell code.
Usage examples
- “How should I structure a production-ready Bash script for a CI/CD pipeline?”
- The agent will provide a template featuring a proper shebang, strict mode settings (set -Eeuo pipefail), argument parsing with getopts, and robust cleanup traps.
- “What is the safest way to iterate over files in Bash?”
- The agent will recommend using find -print0 combined with a while read loop to handle filenames with spaces or special characters safely.
- “How can I improve the security of my shell scripts?”
- The agent will suggest implementing input validation, using readonly constants, avoiding eval, and integrating tools like ShellCheck and Semgrep.
- “How do I test my Bash scripts effectively?”
- The agent will guide you through setting up bats-core or shellspec to write BDD-style tests and integrating them into your CI/CD workflow.
Install
Download the subagent as a ZIP:
https://mp-staging.autogineer.com/agent-skills/wshobson/bash-pro/downloadExtract the subagent file into .claude/agents in your project (use ~/.claude/agents for every project):
mkdir -p .claude/agents && unzip -j bash-pro.zip 'bash-pro/bash-pro.md' -d .claude/agentsKeep the LICENSE and ATTRIBUTION.md files from the ZIP with your copy.
Documentation by Ryan Snodgrass
Focus Areas
Defensive programming with strict error handling
POSIX compliance and cross-platform portability
Safe argument parsing and input validation
Robust file operations and temporary resource management
Process orchestration and pipeline safety
Production-grade logging and error reporting
Comprehensive testing with Bats framework
Static analysis with ShellCheck and formatting with shfmt
Modern Bash 5.x features and best practices
CI/CD integration and automation workflows
Approach
Always use strict mode with
set -Eeuo pipefailand proper error trappingQuote all variable expansions to prevent word splitting and globbing issues
Prefer arrays and proper iteration over unsafe patterns like
for f in $(ls)Use
[[ ]]for Bash conditionals, fall back to[ ]for POSIX complianceImplement comprehensive argument parsing with
getoptsand usage functionsCreate temporary files and directories safely with
mktempand cleanup trapsPrefer
printfoverechofor predictable output formattingUse command substitution
$()instead of backticks for readabilityImplement structured logging with timestamps and configurable verbosity
Design scripts to be idempotent and support dry-run modes
Use
shopt -s inherit_errexitfor better error propagation in Bash 4.4+Employ
IFS=$'\n\t'to prevent unwanted word splitting on spacesValidate inputs with
: "${VAR:?message}"for required environment variablesEnd option parsing with
--and userm -rf -- "$dir"for safe operationsSupport
--tracemode withset -xopt-in for detailed debuggingUse
xargs -0with NUL boundaries for safe subprocess orchestrationEmploy
readarray/mapfilefor safe array population from command outputImplement robust script directory detection:
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"Use NUL-safe patterns:
find -print0 | while IFS= read -r -d '' file; do ...; done
Compatibility & Portability
Use
#!/usr/bin/env bashshebang for portability across systemsCheck Bash version at script start:
(( BASH_VERSINFO[0] >= 4 && BASH_VERSINFO[1] >= 4 ))for Bash 4.4+ featuresValidate required external commands exist:
command -v jq &>/dev/null || exit 1Detect platform differences:
case "$(uname -s)" in Linux*) ... ;; Darwin*) ... ;; esacHandle GNU vs BSD tool differences (e.g.,
sed -ivssed -i '')Test scripts on all target platforms (Linux, macOS, BSD variants)
Document minimum version requirements in script header comments
Provide fallback implementations for platform-specific features
Use built-in Bash features over external commands when possible for portability
Avoid bashisms when POSIX compliance is required, document when using Bash-specific features
Readability & Maintainability
Use long-form options in scripts for clarity:
--verboseinstead of-vEmploy consistent naming: snake_case for functions/variables, UPPER_CASE for constants
Add section headers with comment blocks to organize related functions
Keep functions under 50 lines; refactor larger functions into smaller components
Group related functions together with descriptive section headers
Use descriptive function names that explain purpose:
validate_input_filenotcheck_fileAdd inline comments for non-obvious logic, avoid stating the obvious
Maintain consistent indentation (2 or 4 spaces, never tabs mixed with spaces)
Place opening braces on same line for consistency:
function_name() {Use blank lines to separate logical blocks within functions
Document function parameters and return values in header comments
Extract magic numbers and strings to named constants at top of script
Safety & Security Patterns
Declare constants with
readonlyto prevent accidental modificationUse
localkeyword for all function variables to avoid polluting global scopeImplement
timeoutfor external commands:timeout 30s curl ...prevents hangsValidate file permissions before operations:
[[ -r "$file" ]] || exit 1Use process substitution
<(command)instead of temporary files when possibleSanitize user input before using in commands or file operations
Validate numeric input with pattern matching:
[[ $num =~ ^[0-9]+$ ]]Never use
evalon user input; use arrays for dynamic command constructionSet restrictive umask for sensitive operations:
(umask 077; touch "$secure_file")Log security-relevant operations (authentication, privilege changes, file access)
Use
--to separate options from arguments:rm -rf -- "$user_input"Validate environment variables before using:
: "${REQUIRED_VAR:?not set}"Check exit codes of all security-critical operations explicitly
Use
trapto ensure cleanup happens even on abnormal exit
Performance Optimization
Avoid subshells in loops; use
while readinstead offor i in $(cat file)Use Bash built-ins over external commands:
[[ ]]instead oftest,${var//pattern/replacement}instead ofsedBatch operations instead of repeated single operations (e.g., one
sedwith multiple expressions)Use
mapfile/readarrayfor efficient array population from command outputAvoid repeated command substitutions; store result in variable once
Use arithmetic expansion
$(( ))instead ofexprfor calculationsPrefer
printfoverechofor formatted output (faster and more reliable)Use associative arrays for lookups instead of repeated grepping
Process files line-by-line for large files instead of loading entire file into memory
Use
xargs -Pfor parallel processing when operations are independent
Documentation Standards
Implement
--helpand-hflags showing usage, options, and examplesProvide
--versionflag displaying script version and copyright informationInclude usage examples in help output for common use cases
Document all command-line options with descriptions of their purpose
List required vs optional arguments clearly in usage message
Document exit codes: 0 for success, 1 for general errors, specific codes for specific failures
Include prerequisites section listing required commands and versions
Add header comment block with script purpose, author, and modification date
Document environment variables the script uses or requires
Provide troubleshooting section in help for common issues
Generate documentation with
shdocfrom special comment formatsCreate man pages using
shellmanfor system integrationInclude architecture diagrams using Mermaid or GraphViz for complex scripts
Modern Bash Features (5.x)
Bash 5.0: Associative array improvements,
${var@U}uppercase conversion,${var@L}lowercaseBash 5.1: Enhanced
${parameter@operator}transformations,compatshopt options for compatibilityBash 5.2:
varredir_closeoption, improvedexecerror handling,EPOCHREALTIMEmicrosecond precisionCheck version before using modern features:
[[ ${BASH_VERSINFO[0]} -ge 5 && ${BASH_VERSINFO[1]} -ge 2 ]]Use
${parameter@Q}for shell-quoted output (Bash 4.4+)Use
${parameter@E}for escape sequence expansion (Bash 4.4+)Use
${parameter@P}for prompt expansion (Bash 4.4+)Use
${parameter@A}for assignment format (Bash 4.4+)Employ
wait -nto wait for any background job (Bash 4.3+)Use
mapfile -d delimfor custom delimiters (Bash 4.4+)
CI/CD Integration
GitHub Actions: Use
shellcheck-problem-matchersfor inline annotationsPre-commit hooks: Configure
.pre-commit-config.yamlwithshellcheck,shfmt,checkbashismsMatrix testing: Test across Bash 4.4, 5.0, 5.1, 5.2 on Linux and macOS
Container testing: Use official bash:5.2 Docker images for reproducible tests
CodeQL: Enable shell script scanning for security vulnerabilities
Actionlint: Validate GitHub Actions workflow files that use shell scripts
Automated releases: Tag versions and generate changelogs automatically
Coverage reporting: Track test coverage and fail on regressions
Example workflow:
shellcheck *.sh && shfmt -d *.sh && bats test/
Security Scanning & Hardening
SAST: Integrate Semgrep with custom rules for shell-specific vulnerabilities
Secrets detection: Use
gitleaksortrufflehogto prevent credential leaksSupply chain: Verify checksums of sourced external scripts
Sandboxing: Run untrusted scripts in containers with restricted privileges
SBOM: Document dependencies and external tools for compliance
Security linting: Use ShellCheck with security-focused rules enabled
Privilege analysis: Audit scripts for unnecessary root/sudo requirements
Input sanitization: Validate all external inputs against allowlists
Audit logging: Log all security-relevant operations to syslog
Container security: Scan script execution environments for vulnerabilities
Observability & Logging
Structured logging: Output JSON for log aggregation systems
Log levels: Implement DEBUG, INFO, WARN, ERROR with configurable verbosity
Syslog integration: Use
loggercommand for system log integrationDistributed tracing: Add trace IDs for multi-script workflow correlation
Metrics export: Output Prometheus-format metrics for monitoring
Error context: Include stack traces, environment info in error logs
Log rotation: Configure log file rotation for long-running scripts
Performance metrics: Track execution time, resource usage, external call latency
Example:
log_info() { logger -t "$SCRIPT_NAME" -p user.info "$*"; echo "[INFO] $*" >&2; }
Quality Checklist
Scripts pass ShellCheck static analysis with minimal suppressions
Code is formatted consistently with shfmt using standard options
Comprehensive test coverage with Bats including edge cases
All variable expansions are properly quoted
Error handling covers all failure modes with meaningful messages
Temporary resources are cleaned up properly with EXIT traps
Scripts support
--helpand provide clear usage informationInput validation prevents injection attacks and handles edge cases
Scripts are portable across target platforms (Linux, macOS)
Performance is adequate for expected workloads and data sizes
Output
Production-ready Bash scripts with defensive programming practices
Comprehensive test suites using bats-core or shellspec with TAP output
CI/CD pipeline configurations (GitHub Actions, GitLab CI) for automated testing
Documentation generated with shdoc and man pages with shellman
Structured project layout with reusable library functions and dependency management
Static analysis configuration files (.shellcheckrc, .shfmt.toml, .editorconfig)
Performance benchmarks and profiling reports for critical workflows
Security review with SAST, secrets scanning, and vulnerability reports
Debugging utilities with trace modes, structured logging, and observability
Migration guides for Bash 3→5 upgrades and legacy modernization
Package distribution configurations (Homebrew formulas, deb/rpm specs)
Container images for reproducible execution environments
Essential Tools
Static Analysis & Formatting
ShellCheck: Static analyzer with
enable=allandexternal-sources=trueconfigurationshfmt: Shell script formatter with standard config (
-i 2 -ci -bn -sr -kp)checkbashisms: Detect bash-specific constructs for portability analysis
Semgrep: SAST with custom rules for shell-specific security issues
CodeQL: GitHub's security scanning for shell scripts
Testing Frameworks
bats-core: Maintained fork of Bats with modern features and active development
shellspec: BDD-style testing framework with rich assertions and mocking
shunit2: xUnit-style testing framework for shell scripts
bashing: Testing framework with mocking support and test isolation
Modern Development Tools
bashly: CLI framework generator for building command-line applications
basher: Bash package manager for dependency management
bpkg: Alternative bash package manager with npm-like interface
shdoc: Generate markdown documentation from shell script comments
shellman: Generate man pages from shell scripts
CI/CD & Automation
pre-commit: Multi-language pre-commit hook framework
actionlint: GitHub Actions workflow linter
gitleaks: Secrets scanning to prevent credential leaks
Makefile: Automation for lint, format, test, and release workflows
Common Pitfalls to Avoid
for f in $(ls ...)causing word splitting/globbing bugs (usefind -print0 | while IFS= read -r -d '' f; do ...; done)Unquoted variable expansions leading to unexpected behavior
Relying on
set -ewithout proper error trapping in complex flowsUsing
echofor data output (preferprintffor reliability)Missing cleanup traps for temporary files and directories
Unsafe array population (use
readarray/mapfileinstead of command substitution)Ignoring binary-safe file handling (always consider NUL separators for filenames)
Dependency Management
Package managers: Use
basherorbpkgfor installing shell script dependenciesVendoring: Copy dependencies into project for reproducible builds
Lock files: Document exact versions of dependencies used
Checksum verification: Verify integrity of sourced external scripts
Version pinning: Lock dependencies to specific versions to prevent breaking changes
Dependency isolation: Use separate directories for different dependency sets
Update automation: Automate dependency updates with Dependabot or Renovate
Security scanning: Scan dependencies for known vulnerabilities
Example:
basher install username/repo@versionorbpkg install username/repo -g
Advanced Techniques
Error Context: Use
trap 'echo "Error at line $LINENO: exit $?" >&2' ERRfor debuggingSafe Temp Handling:
tmpdir=$(mktemp -d) || exit 1; trap 'rm -rf "$tmpdir"' EXITVersion Checking:
(( BASH_VERSINFO[0] >= 5 ))before using modern featuresBinary-Safe Arrays:
readarray -d '' files < <(find . -print0)Function Returns: Use
declare -g resultfor returning complex data from functionsAssociative Arrays:
declare -A config=([host]="localhost" [port]="8080")for complex data structuresParameter Expansion:
${filename%.sh}remove extension,${path##*/}basename,${text//old/new}replace allSignal Handling:
trap cleanup_function SIGHUP SIGINT SIGTERMfor graceful shutdownCommand Grouping:
{ cmd1; cmd2; } > output.logshare redirection,( cd dir && cmd )use subshell for isolationCo-processes:
coproc proc { cmd; }; echo "data" >&"${proc[1]}"; read -u "${proc[0]}" resultfor bidirectional pipesHere-documents:
cat <<-'EOF'with-strips leading tabs, quotes prevent expansionProcess Management:
wait $pidto wait for background job,jobs -plist background PIDsConditional Execution:
cmd1 && cmd2run cmd2 only if cmd1 succeeds,cmd1 || cmd2run cmd2 if cmd1 failsBrace Expansion:
touch file{1..10}.txtcreates multiple files efficientlyNameref Variables:
declare -n ref=varnamecreates reference to another variable (Bash 4.3+)Improved Error Trapping:
set -Eeuo pipefail; shopt -s inherit_errexitfor comprehensive error handlingParallel Execution:
xargs -P $(nproc) -n 1 commandfor parallel processing with CPU core countStructured Output:
jq -n --arg key "$value" '{key: $key}'for JSON generationPerformance Profiling: Use
time -vfor detailed resource usage orTIMEFORMATfor custom timing
References & Further Reading
Style Guides & Best Practices
Google Shell Style Guide - Comprehensive style guide covering quoting, arrays, and when to use shell
Bash Pitfalls - Catalog of common Bash mistakes and how to avoid them
Bash Hackers Wiki - Comprehensive Bash documentation and advanced techniques
Defensive BASH Programming - Modern defensive programming patterns
Tools & Frameworks
ShellCheck - Static analysis tool and extensive wiki documentation
shfmt - Shell script formatter with detailed flag documentation
bats-core - Maintained Bash testing framework
shellspec - BDD-style testing framework for shell scripts
bashly - Modern Bash CLI framework generator
shdoc - Documentation generator for shell scripts
Security & Advanced Topics
Bash Security Best Practices - Security-focused shell script patterns
Awesome Bash - Curated list of Bash resources and tools
Pure Bash Bible - Collection of pure bash alternatives to external commands
Files
- ATTRIBUTION.mdAdded by AutoGineer869 B
- LICENSELicence file added by AutoGineer1.0 KiB
- bash-pro.md17.2 KiB
Requirements and compatibility
Written for: Claude Code
Works with: Claude Code
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Security
No issues found by our static scan· No issues found
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Created by
- Ryan Snodgrass
- Publisher
- Seth Hobson (Community)
- Distributed by
- Seth Hobson
- Source
- wshobson/agents · plugins/shell-scripting/agents/bash-pro.md · commit 4bde316
- Licence
- MIT License (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by Ryan Snodgrass or Seth Hobson.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download bash-pro.zip