bash-defensive-patterns

Apply defensive programming patterns to Bash scripts for runtime failure prevention.

1|Updated Apr 27, 2026
One-click install
npx skills add https://github.com/haxlys/skills --skill bash-defensive-patterns-haxlys
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: bash-defensive-patterns
Source: https://github.com/haxlys/skills/tree/main/vendored/wshobson-agents/plugins/shell-scripting/skills/bash-defensive-patterns
Command: npx skills add https://github.com/haxlys/skills --skill bash-defensive-patterns-haxlys

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Production Bash scripts often fail quietly or behave inconsistently. This Skill consolidates defensive programming techniques to enforce reliability, safety, and predictable failure modes across automation tasks.

Core Features & Use Cases

  • Strict mode: enable set -Eeuo pipefail to catch errors early and propagate failures.
  • Error trapping and cleanup: implement traps to log, cleanup, and gracefully exit on errors.
  • Variable safety and quoting: consistently quote variables to prevent word-splitting and expansion issues.
  • Robust patterns: safe directory operations, atomic writes, and idempotent design for repeatable runs.
  • Use Case: apply these patterns to CI/CD scripts, deployment tasks, or system administration utilities to reduce bugs and improve reliability.

Quick Start

Start by applying these patterns to your Bash scripts to enforce strict mode and safe error handling in every automation task.

Frequently Asked Questions about bash-defensive-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I make bash scripts fail safely in production automation?

Make bash scripts fail safely by enabling strict mode with set -Eeuo pipefail to catch errors early and propagate failures consistently. This enforces reliable execution and prevents unpredictable behavior in production environments.

What is the best way to handle errors and cleanup in shell scripting?

The best way to handle errors in shell scripting is implementing traps to log failures, cleanup resources, and gracefully exit on errors. This ensures predictable failure modes and prevents inconsistent state.

How do I prevent word-splitting and expansion issues in bash variables?

Prevent word-splitting and expansion issues in bash variables by consistently quoting variables throughout your scripts. This variable safety practice ensures proper handling of spaces and special characters.

Can I apply defensive programming patterns to CI/CD deployment pipelines?

Yes, you can apply defensive programming patterns to CI/CD deployment pipelines and system administration utilities. These patterns enforce strict mode and safe error handling to reduce bugs and improve reliability.

Why does my bash script fail quietly without throwing an error?

Bash scripts often fail quietly because strict mode is not enabled. Applying defensive patterns like pipefail and error trapping catches silent failures early, transforming them into predictable and logged exits.

How do I ensure repeatable runs with idempotent directory operations in bash?

Ensure repeatable runs in bash by using robust patterns for safe directory operations, atomic writes, and idempotent design. This approach guarantees that scripts execute consistently across multiple deployments.