bash-defensive-patterns
What it does
This guide teaches how to write safer and more reliable automation scripts (Bash). It helps prevent common errors and ensures your scripts work correctly even in unexpected situations.
When to use it
- ✓When creating scripts to automate important tasks at work.
- ✓When building automation steps for development processes (CI/CD).
- ✓When developing system tools that need to be very reliable.
Skill link
https://github.com/wshobson/agents/blob/main/plugins/shell-scripting/skills/bash-defensive-patterns/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agent’s skills folder.
Example of what to ask your AI
“How can I write a Bash script that won't fail if a file doesn't exist?”
5 new skills a week. 1 minute to read.
How it works
It uses techniques like enabling safety modes (`set -euo pipefail`), always protecting variables with quotes (`"$variable"`), and checking if commands executed successfully.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
Bash 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
FAQ
What is 'strict mode'?
It's a set of rules (`set -Eeuo pipefail`) that makes the script more rigorous, forcing the correction of common errors that might otherwise go unnoticed.
Why should I quote variables?
Putting quotes around variables (`"$variable"`) prevents the script from misinterpreting spaces or special characters within the variable's value, thus avoiding errors.
What does 'idempotency' mean?
An idempotent script can be run multiple times without causing unintended side effects. It ensures the final result is the same, no matter how many times the script is executed.
Details
Original description by the author
Master defensive Bash programming techniques for production-grade scripts. Use when writing robust shell scripts, CI/CD pipelines, or system utilities requiring fault tolerance and safety.
repo: wshobson/agents
Popularity
- GitHub stars
- 41k
- Ranking position
- #6783
- License
- MIT
- Updated
- 10/15/2026
Comments
- …
More skills from this repository
screen-reader-testing
41kHas your agent test websites using screen readers to ensure they are accessible.
wcag-audit-patterns
41kHelps your agent audit websites for accessibility and fix issues according to WCAG guidelines.
multi-reviewer-patterns
41kEnables your agent to coordinate parallel code reviews, deduplicating findings and consolidating reports.
parallel-debugging
41kAllows the agent to debug complex issues with parallel investigations and root cause analysis.
Similar skills
diffs
394kLets your agent produce a real, shareable diff of edits as a viewer link or image file, instead of a hand-written summary.
coding-agent
394kHelps your agent hand a larger coding job to Codex, Claude Code, or OpenCode as a background worker, then check on its progress.
gh-issues
394kHelps your agent find GitHub issues, pick the ones to work on, and open pull requests with fixes, including follow-up on review comments.