Bash Defensive Patterns
Seth HobsonCommunityCreated by Ryan SnodgrassDistributed by Seth Hobson
- Skill
- Guidelines
- Software development
- MIT
- No issues found
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This skill provides a structured approach to writing robust Bash scripts by implementing defensive programming patterns. It focuses on error handling, safety, and maintainability, ensuring that scripts are resilient to edge cases, prevent common pitfalls like word splitting, and behave predictably in production environments. The content covers essential practices such as enabling strict mode, implementing proper cleanup with traps, safe variable handling, and robust argument parsing.
What it does
- Defines strict mode settings to catch errors early
- Provides patterns for safe temporary file and directory management
- Demonstrates robust error trapping and cleanup techniques
- Outlines best practices for variable quoting and conditional logic
- Offers templates for structured function design and argument parsing
- Encourages idempotent and testable script design
When to use it
- When developing production-grade automation scripts
- When writing scripts for CI/CD pipelines
- When creating system administration utilities
- When building scripts that require high reliability and fault tolerance
Advantages
- Reduces runtime errors through strict mode and input validation
- Improves script maintainability and readability
- Ensures predictable behavior across different environments
- Prevents common security and stability issues like word splitting
Usage examples
- “How do I ensure my Bash script stops immediately if a command fails?”
- The skill recommends enabling strict mode by adding 'set -Eeuo pipefail' at the beginning of the script to ensure it exits on errors, unset variables, and pipe failures.
- “What is the safest way to handle temporary files in a script?”
- The skill provides a pattern using 'mktemp -d' to create a unique directory and a 'trap' command to ensure the directory is automatically removed upon script exit.
Install
Download the skill as a ZIP:
https://mp-staging.autogineer.com/agent-skills/wshobson/bash-defensive-patterns/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip bash-defensive-patterns.zip -d .agents/skills
Documentation by Ryan Snodgrass
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 pipefailQuote all variables -
"$variable"prevents word splittingUse [[]] 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 executablesPrefer printf over echo - More predictable across systems
Files
- ATTRIBUTION.mdAdded by AutoGineer896 B
- LICENSELicence file added by AutoGineer1.0 KiB
- SKILL.md1.9 KiB
- references/details.md9.8 KiB
Requirements and compatibility
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“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/skills/bash-defensive-patterns · 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-defensive-patterns.zip