
Bash Defensive Patterns
FreeEnhance your Bash scripts with robust defensive techniques.
Free · Opens the source repo
What Bash Defensive Patterns does
Bash Defensive Patterns provides a comprehensive framework for writing reliable and production-ready Bash scripts. This skill emphasizes defensive programming techniques that help prevent common pitfalls, making it ideal for developers and system administrators who need to ensure their scripts are resilient and maintainable. By following the outlined best practices, users can significantly reduce the risk of errors in their automation tasks, CI/CD pipelines, and system utilities.
The skill covers essential concepts such as strict mode (set -Eeuo pipefail), input validation, and error handling, which are critical for developing scripts that function correctly under various conditions. It also highlights the importance of logging and monitoring, ensuring that users can track the execution of their scripts and diagnose issues effectively. With a focus on cross-platform compatibility, the patterns provided in this skill are designed to work seamlessly across different environments, making it a versatile addition to any developer's toolkit.
For those looking to deepen their understanding of Bash scripting, the skill includes detailed patterns and worked examples that can be found in the accompanying references/details.md file. This resource serves as a valuable reference for both beginners and experienced developers, providing insights into best practices and advanced techniques that enhance script reliability and maintainability.
When to use it
Use this skill when writing production automation scripts, building CI/CD pipelines, or creating system administration utilities that require robust error handling.
When not to use it
This skill may not be suitable for simple scripts or one-off tasks where error handling and robustness are not a priority.
What you can build with it
Automating Server Deployments
Use this skill to write scripts that automate server deployments, ensuring they handle errors and edge cases effectively.
Building CI/CD Pipelines
Implement defensive programming techniques in your CI/CD pipeline scripts to enhance reliability and reduce failures during automated builds.
Creating System Utilities
Develop system utilities that require robust logging and error handling to ensure they operate safely across different environments.
How to install Bash Defensive Patterns
View source1. Install with the skills CLI
npx skills add wshobson/agents/bash-defensive-patterns --agent claude-code2. Or install it manually
Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.
Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs
Inside SKILL.md
Written by wshobsonBash 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
Frequently asked questions about Bash Defensive Patterns
Similar skills
Turborepo
Optimized build system for JavaScript/TypeScript monorepos.
Azure Pipelines Validation
Streamline your Azure DevOps pipeline changes locally.
Azure Developer CLI
Streamline your Azure project workflows with best practices.
Azure Container Registry CLI
Manage Azure Container Registry resources with ease.
Aspire
Build and orchestrate polyglot distributed applications seamlessly.
Vercel CLI
Manage and deploy Vercel projects from the command line.
