bash-defensive-patterns
Teaches your agent to write secure and reliable command-line scripts. It's useful for creating tools that work well and avoid errors in important systems.
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.
Example of what to ask your AI
“How can I write a Bash script that won't fail if a file doesn't exist?”
Want more? Open the sections below.
How to use
The easiest way: copy the message below and paste it into Claude, ChatGPT, Cursor or any AI. It reads the skill and follows the instructions.
Skill link
Ready-to-paste message
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.
Installation
Easiest way: copy the skill link and paste it into Claude, ChatGPT or Cursor asking it to follow the instructions.
In the terminal (Claude Code, Codex, Cursor), in your project folder:
npx skills add wshobson/agents --skill bash-defensive-patternsOr download just the file:
curl -L https://raw.githubusercontent.com/wshobson/agents/main/plugins/shell-scripting/skills/bash-defensive-patterns/SKILL.md -o SKILL.mdTechnical 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
- #4950
- License
- MIT
- Updated
- 10/15/2026
Comments
- …
More skills from this repository
screen-reader-testing
Has your agent test websites using screen readers to ensure they are accessible.
wshobson/agents
wcag-audit-patterns
Helps your agent audit websites for accessibility and fix issues according to WCAG guidelines.
wshobson/agents
multi-reviewer-patterns
Enables your agent to coordinate parallel code reviews, deduplicating findings and consolidating reports.
wshobson/agents
parallel-debugging
Allows the agent to debug complex issues with parallel investigations and root cause analysis.
wshobson/agents
Similar skills
benchmark-optimization-loop
Has your agent improve something's speed in a measured way. It tests multiple versions and chooses the fastest one that still works correctly.
affaan-m/ECC
change-impact
Helps your agent analyze the impact of code changes before making them. It compares the planned vs. actual results afterward, covering paths, data, tests, and documentation. Useful for risky or system-wide changes.
affaan-m/ECC
click-path-audit
Lets your agent trace all user buttons and interactions to find bugs. It finds issues where functions seem to work alone but fail together. Useful when bugs persist even after debugging.
affaan-m/ECC