About bash-defensive-patterns
A skill for writing reliable Bash scripts through defensive programming. It covers error handling, validation, logging, safe temporary files, and idempotent operations for automation and system administration.
Key features
- 01Strict mode and error trapping
- 02Quoting and input validation
- 03Structured logging and dry runs
- 04Safe temporary file handling
- 05Reusable functions and idempotency
Use cases
- 01Writing production automation scripts
- 02Building CI/CD pipeline scripts
- 03Developing system administration utilities
- 04Creating safely repeatable deployment scripts
Install
Copy the command and run it in a terminal.
$
npx skills add https:/ / github.com/ wshobson/ agents --skill bash-defensive-patternsAfter installing, type/bash-defensive-patternsin Claude Code to confirm it loaded.
Bundled filesreferences/details.md
Details
From the project READMEBash Defensive Patterns
Comprehensive guidance for writing production-ready Bash scripts using defensive programming techniques, error handling, and safety best practices to prevent common pitfalls and ensure reliability.
When to Use This Skill
- Writing production automation scripts
- Building CI/CD pipeline scripts
- Creating system administration utilities
- Developing error-resilient deployment automation
- Writing scripts that must handle edge cases safely
- Building maintainable shell script libraries
- Implementing comprehensive logging and monitoring
- Creating scripts that must work across different platforms
Detailed patterns and worked examples
Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.
Best Practices Summary
- Always use strict mode -
set -Eeuo pipefail - Quote all variables -
"$variable"prevents word splitting - Use [[]] conditionals - More robust than [ ]
- Implement error trapping - Catch and handle errors gracefully
- Validate all inputs - Check file existence, permissions, formats
- Use functions for reusability - Prefix with meaningful names
- Implement structured logging - Include timestamps and levels
- Support dry-run mode - Allow users to preview changes
- Handle temporary files safely - Use mktemp, cleanup with trap
- Design for idempotency - Scripts should be safe to rerun
- Document requirements - List dependencies and minimum versions
- Test error paths - Ensure error handling works correctly
- Use
command -v- Safer thanwhichfor checking executables - Prefer printf over echo - More predictable across systems
Comments
Markdown supported