Bash Defensive Patterns

by wshobson46891e7e60daNo licenseListed Oct 8, 2026Updated Oct 8, 2026

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.

Instructions onlySoftware Development
AI-generated overview

Guidance for writing robust, fault-tolerant Bash scripts using defensive programming and error handling.

What it does
This skill provides best-practice guidance for writing production-grade Bash scripts with defensive programming techniques. It covers strict mode, variable quoting, error trapping, input validation, structured logging, dry-run support, safe temporary files, and idempotency. Detailed patterns and worked examples are kept in a separate reference file that can be read when the summary is not enough.
When to use it
Use it when writing production automation scripts, CI/CD pipeline scripts, system administration utilities, or deployment automation that must handle edge cases safely. It is also suited to maintainable shell script libraries and scripts that need cross-platform reliability.
Requirements
No scripts are shipped; it is instructions only. Reading the bundled reference file requires file access, but no packages, runtimes, credentials, or network access are needed.

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

  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

Source and attribution

Source:wshobson/agentsinplugins/shell-scripting/skills/bash-defensive-patternsat commit46891e7

License: No license

Content belongs to its original authors. SourceWeft indexes it from a public repository.

Report or request removal