bash-defensive-patterns

Enforce strict mode, error trapping, and variable safety in Bash scripts.

Updated Feb 26, 2026
One-click install
npx skills add https://github.com/AndyAnh174/wellness --skill bash-defensive-patterns-andyanh174
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: bash-defensive-patterns
Source: https://github.com/AndyAnh174/wellness/tree/main/.agent/skills/bash-defensive-patterns
Command: npx skills add https://github.com/AndyAnh174/wellness --skill bash-defensive-patterns-andyanh174

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Defensive Bash patterns provide comprehensive guidance for writing production-ready Bash scripts by applying defensive programming techniques, robust error handling, and safety best practices to prevent common pitfalls and improve reliability.

Core Features & Use Cases

  • Strict mode: enable set -Eeuo pipefail to catch errors early and enforce safer scripting.
  • Error trapping and cleanup: implement traps and cleanup routines to ensure graceful exits and proper resource management.
  • Variable safety and quoting: consistently quote variables to prevent word-splitting and globbing issues.
  • Safe patterns and templates: reusable patterns for file handling, argument parsing, and input validation.
  • Idempotent design: build scripts that can be rerun without unintended side effects.

Quick Start

Enable bash strict mode at the start of every script and begin applying the defensive patterns to your automation tasks.

Frequently Asked Questions about bash-defensive-patterns

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

FAQPage Schema
How do I write robust Bash scripts that handle errors safely in automation?

You can make Bash scripts fault-tolerant by enabling strict mode with set -Eeuo pipefail, implementing error traps, and applying safe variable quoting to handle errors safely.

What is bash strict mode and how does it prevent scripting errors?

Bash strict mode uses set -Eeuo pipefail to catch errors early, enforce safer scripting practices, and prevent common pitfalls like uninitialized variables and silent pipe failures.

How do I implement error trapping and cleanup routines in shell scripts?

Implement error trapping in shell scripts by setting traps that intercept errors and execute cleanup routines, ensuring graceful exits and proper resource management during failures.

Why does my Bash script fail when variables are unquoted during file operations?

Bash scripts fail from unquoted variables because word-splitting and globbing issues corrupt file paths; consistently quoting variables provides variable safety and prevents unintended side effects.

Can I use defensive programming patterns for CI/CD pipelines and system administration utilities?

Yes, defensive programming patterns apply directly to CI/CD pipelines and system administration utilities, providing reusable templates for input validation, argument parsing, and safe file handling.

What's the best way to build idempotent Bash scripts that can be rerun safely?

The best way to build idempotent Bash scripts is applying defensive design templates that manage state safely, ensuring scripts can be rerun without unintended side effects or duplicate executions.