New to Claude Skills? Learn how to install them →

Bwshobson on GitHub

Bash Defensive Patterns

Free

Enhance your Bash scripts with robust defensive techniques.

Get this skill

Free · Opens the source repo

What Bash Defensive Patterns does

Bash Defensive Patterns provides a comprehensive framework for writing reliable and production-ready Bash scripts. This skill emphasizes defensive programming techniques that help prevent common pitfalls, making it ideal for developers and system administrators who need to ensure their scripts are resilient and maintainable. By following the outlined best practices, users can significantly reduce the risk of errors in their automation tasks, CI/CD pipelines, and system utilities.

The skill covers essential concepts such as strict mode (set -Eeuo pipefail), input validation, and error handling, which are critical for developing scripts that function correctly under various conditions. It also highlights the importance of logging and monitoring, ensuring that users can track the execution of their scripts and diagnose issues effectively. With a focus on cross-platform compatibility, the patterns provided in this skill are designed to work seamlessly across different environments, making it a versatile addition to any developer's toolkit.

For those looking to deepen their understanding of Bash scripting, the skill includes detailed patterns and worked examples that can be found in the accompanying references/details.md file. This resource serves as a valuable reference for both beginners and experienced developers, providing insights into best practices and advanced techniques that enhance script reliability and maintainability.

When to use it

Use this skill when writing production automation scripts, building CI/CD pipelines, or creating system administration utilities that require robust error handling.

When not to use it

This skill may not be suitable for simple scripts or one-off tasks where error handling and robustness are not a priority.

What you can build with it

Automating Server Deployments

Use this skill to write scripts that automate server deployments, ensuring they handle errors and edge cases effectively.

Building CI/CD Pipelines

Implement defensive programming techniques in your CI/CD pipeline scripts to enhance reliability and reduce failures during automated builds.

Creating System Utilities

Develop system utilities that require robust logging and error handling to ensure they operate safely across different environments.

How to install Bash Defensive Patterns

View source

1. Install with the skills CLI

npx skills add wshobson/agents/bash-defensive-patterns --agent claude-code

2. Or install it manually

Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.

Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs

Inside SKILL.md

Written by wshobson

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

Frequently asked questions about Bash Defensive Patterns

Similar skills