bash-defensive-patterns

Enforce defensive Bash patterns with strict mode and error traps.

41|6|Updated Nov 27, 2025
One-click install
npx skills add https://github.com/mralexsaavedra/spotiarr --skill bash-defensive-patterns-mralexsaavedra
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: bash-defensive-patterns
Source: https://github.com/mralexsaavedra/spotiarr/tree/main/skills/bash-defensive-patterns
Command: npx skills add https://github.com/mralexsaavedra/spotiarr --skill bash-defensive-patterns-mralexsaavedra

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps you prevent common Bash scripting failures (silent errors, unsafe word splitting, broken cleanup, and fragile argument handling) by applying production-grade defensive programming patterns.

Core Features & Use Cases

  • Strict mode and fail-fast error handling: Use set -Eeuo pipefail plus consistent error/exit traps to stop early and keep failures diagnosable.
  • Safe variables and robust quoting: Avoid word splitting and globbing issues, and enforce required variables with clear error messages.
  • Idempotent and production-friendly patterns: Build reusable function templates for validation, safe temp files, atomic writes, and dependable orchestration (including signal-aware cleanup).
  • Use cases: Harden CI/CD shell scripts, system utilities, deployment automation, and maintenance scripts that must be reliable across edge cases and reruns.

Quick Start

Apply strict mode, add ERR/EXIT traps, and refactor your script to validate inputs, quote every variable, and implement safe temp/atomic operations before you ship it.

Frequently Asked Questions about bash-defensive-patterns

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

FAQPage Schema
How do I prevent silent failures in bash scripts?

Prevent silent failures in bash scripts by enforcing strict mode with `set -Eeuo pipefail` and consistent error traps to stop execution early and keep failures diagnosable, ensuring errors are caught immediately rather than ignored silently.

What's the best way to handle cleanup and temp files in shell scripting?

Handle cleanup and temp files in shell scripting by implementing signal-aware EXIT and ERR traps alongside atomic write operations. This pattern guarantees that temporary resources are removed safely even when scripts fail or receive termination signals.

How do I make CI/CD deployment scripts idempotent and rerunnable?

Make CI/CD deployment scripts idempotent by applying defensive programming patterns that validate inputs, enforce required variables, and use guarded filesystem operations. This ensures maintenance tooling remains reliable across edge cases and repeated executions.

Why does word splitting break my bash variables and how do I fix it?

Word splitting breaks bash variables when unquoted expansions fragment strings into multiple arguments. Fix it by applying robust quoting patterns and safe array handling to prevent globbing issues and preserve variable correctness.

Does this defensive programming approach work for system utilities and maintenance scripts?

Yes, this defensive programming approach works for system utilities and maintenance scripts by enforcing robust argument parsing, safe quoting, and consistent error trapping. It provides production-grade guardrails suitable for reliable automation contexts.