SkillLow riskUnclaimed

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.

wshobsonwshobson/bash-defensive-patterns★ 40kUpdated Oct 4, 2026

Description

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

  1. Always use strict mode - set -Eeuo pipefail
  2. Quote all variables - "$variable" prevents word splitting
  3. Use [[]] conditionals - More robust than [ ]
  4. Implement error trapping - Catch and handle errors gracefully
  5. Validate all inputs - Check file existence, permissions, formats
  6. Use functions for reusability - Prefix with meaningful names
  7. Implement structured logging - Include timestamps and levels
  8. Support dry-run mode - Allow users to preview changes
  9. Handle temporary files safely - Use mktemp, cleanup with trap
  10. Design for idempotency - Scripts should be safe to rerun
  11. Document requirements - List dependencies and minimum versions
  12. Test error paths - Ensure error handling works correctly
  13. Use command -v - Safer than which for checking executables
  14. Prefer printf over echo - More predictable across systems

Permissions

DeclaredDetected
Runs code—None
Installs—None
Runs install scripts—None
Network—None
Needs credentials—None
Outside the workspace—None
Agent tools—None

Checks

Low risk · Nothing worth a warning was found.

Not reviewed by a person · Checked by rules; the model review is not switched on yet.

7 minor marks: common commands and the like, noted but not a concern
  • Rule · command_injectionreferences/details.md:319
  • Rule · command_injectionreferences/details.md:345
  • Rule · command_injectionreferences/details.md:30
  • Rule · command_injectionreferences/details.md:150
  • Rule · command_injectionreferences/details.md:332
  • Rule · resource_abusereferences/details.md:275
  • Rule · resource_abusereferences/details.md:279

Files2 files · 11.5 KB

  • SKILL.md1.9 KB
references/1
  • details.md9.5 KB

Versions

  1. #1—latestOct 7, 2026