Master defensive Bash programming techniques
Bash defensive patterns

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.
--- name: bash-defensive-patterns description: 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. --- # 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
Overview
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.
Manual install (npx, ZIP)
npx skills add wshobson/agents --skill bash-defensive-patterns -g -a claude-codemkdir -p ~/.claude/skills && curl -sL https://codeload.github.com/wshobson/agents/tar.gz/4236bb91f839 \
| tar -xz -C ~/.claude/skills --strip-components=4 agents-4236bb91f839/plugins/shell-scripting/skills/bash-defensive-patternsNew-Item -ItemType Directory -Force "$HOME\.claude\skills" | Out-Null; Invoke-WebRequest "https://m.agentalley.io/zip/5b107e94ae50798dde99/bash-defensive-patterns.zip" -OutFile "$env:TEMP\bash-defensive-patterns.zip"; Expand-Archive "$env:TEMP\bash-defensive-patterns.zip" -DestinationPath "$HOME\.claude\skills" -ForceConnects with Agent Alley MCP · agentalley.io/mcp
Files included
SKILL.md1.9 KBreferences/1 file · 9.5 KB
Read the files here
Show the full text41 lines
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
| 1 | |
| 2 | name bash-defensive-patterns |
| 3 | description 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. |
| 4 | |
| 5 | |
| 6 | # Bash Defensive Patterns |
| 7 | |
| 8 | 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. |
| 9 | |
| 10 | ## When to Use This Skill |
| 11 | |
| 12 | Writing production automation scripts |
| 13 | Building CI/CD pipeline scripts |
| 14 | Creating system administration utilities |
| 15 | Developing error-resilient deployment automation |
| 16 | Writing scripts that must handle edge cases safely |
| 17 | Building maintainable shell script libraries |
| 18 | Implementing comprehensive logging and monitoring |
| 19 | Creating scripts that must work across different platforms |
| 20 | |
| 21 | ## Detailed patterns and worked examples |
| 22 | |
| 23 | Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient. |
| 24 | |
| 25 | ## Best Practices Summary |
| 26 | |
| 27 | **Always use strict mode** - `set -Eeuo pipefail` |
| 28 | **Quote all variables** - `"$variable"` prevents word splitting |
| 29 | **Use [[]] conditionals** - More robust than [ ] |
| 30 | **Implement error trapping** - Catch and handle errors gracefully |
| 31 | **Validate all inputs** - Check file existence, permissions, formats |
| 32 | **Use functions for reusability** - Prefix with meaningful names |
| 33 | **Implement structured logging** - Include timestamps and levels |
| 34 | **Support dry-run mode** - Allow users to preview changes |
| 35 | **Handle temporary files safely** - Use mktemp, cleanup with trap |
| 36 | **Design for idempotency** - Scripts should be safe to rerun |
| 37 | **Document requirements** - List dependencies and minimum versions |
| 38 | **Test error paths** - Ensure error handling works correctly |
| 39 | **Use `command -v`** - Safer than `which` for checking executables |
| 40 | **Prefer printf over echo** - More predictable across systems |
| 41 |
Details
- Updated
- Sep 21, 2026
- License
- MIT
- Stars on the repo
- ★ 39,857
- Checked
- Source
- wshobson/agents ↗
- Creator
- wshobson · 179 listings
Similar skills
See all →
SkillCreate branded architecture, architecture deltaDiagram design
SkillReview a PR against the Pascal architectural rulesReview architecture
SkillCreate and edit draw.io/diagrams.net diagramsDraw.io Architecture Studio
SkillCreate polished dark-themed architecture diagramsArchitecture Diagram Skill
SkillThe foundational knowledge distillation pattern for buildingLLM Wiki — Knowledge Distillation Pattern
SkillManage software complexity through deep modulesA Philosophy of Software Design FrameworkMore from wshobson
SkillMaster C/.NET backend development patterns for building
SkillImplement WCAG 2.2 compliant interfaces with mobileAccessibility compliance
SkillMaster Material Design 3 and Jetpack Compose patternsAndroid Mobile Design
SkillMigrate from AngularJS to Angular using hybrid modeAngular migration
SkillUnderstand anti-reversing, obfuscationAnti reversing techniques
SkillBuild production Apache Airflow DAGs with best practicesApache Airflow DAG Patterns