Skip to content
Skillpedia
General coding41k · #4950

bash-defensive-patterns

Teaches your agent to write secure and reliable command-line scripts. It's useful for creating tools that work well and avoid errors in important systems.

What it does

This guide teaches how to write safer and more reliable automation scripts (Bash). It helps prevent common errors and ensures your scripts work correctly even in unexpected situations.

When to use it

  • ✓When creating scripts to automate important tasks at work.
  • ✓When building automation steps for development processes (CI/CD).
  • ✓When developing system tools that need to be very reliable.

Example of what to ask your AI

“How can I write a Bash script that won't fail if a file doesn't exist?”

Want more? Open the sections below.

How to use

The easiest way: copy the message below and paste it into Claude, ChatGPT, Cursor or any AI. It reads the skill and follows the instructions.

Skill link

https://github.com/wshobson/agents/blob/main/plugins/shell-scripting/skills/bash-defensive-patterns/SKILL.md

Ready-to-paste message

Use this skill to help me: https://github.com/wshobson/agents/blob/main/plugins/shell-scripting/skills/bash-defensive-patterns/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
How it works

It uses techniques like enabling safety modes (`set -euo pipefail`), always protecting variables with quotes (`"$variable"`), and checking if commands executed successfully.

Installation

Easiest way: copy the skill link and paste it into Claude, ChatGPT or Cursor asking it to follow the instructions.

In the terminal (Claude Code, Codex, Cursor), in your project folder:

npx skills add wshobson/agents --skill bash-defensive-patterns

Or download just the file:

curl -L https://raw.githubusercontent.com/wshobson/agents/main/plugins/shell-scripting/skills/bash-defensive-patterns/SKILL.md -o SKILL.md
Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

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
FAQ

What is 'strict mode'?

It's a set of rules (`set -Eeuo pipefail`) that makes the script more rigorous, forcing the correction of common errors that might otherwise go unnoticed.

Why should I quote variables?

Putting quotes around variables (`"$variable"`) prevents the script from misinterpreting spaces or special characters within the variable's value, thus avoiding errors.

What does 'idempotency' mean?

An idempotent script can be run multiple times without causing unintended side effects. It ensures the final result is the same, no matter how many times the script is executed.

Details

Original description by 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.

repo: wshobson/agents

Popularity

GitHub stars
41k
Ranking position
#4950
License
MIT
Updated
10/15/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills