Generated banner for Bash Defensive Patterns
Generated logo for Bash Defensive Patterns

Bash Defensive Patterns

Seth HobsonCommunityCreated by Ryan SnodgrassDistributed by Seth Hobson

  • Skill
  • Guidelines
  • Software development
  • MIT
  • No issues found

From 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.
— the description by Ryan Snodgrass

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

  1. Download the skill as a ZIP:

    https://mp-staging.autogineer.com/agent-skills/wshobson/bash-defensive-patterns/download
  2. Unzip 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

  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

Files

SKILL.md

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

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