bash-defensive-patterns

Apply defensive Bash patterns with strict mode, validation, and cleanup traps.

885|108|Updated Apr 30, 2025
One-click install
npx skills add https://github.com/legions-developer/invoicely --skill bash-defensive-patterns-legions-developer
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: bash-defensive-patterns
Source: https://github.com/legions-developer/invoicely/tree/main/.agents/skills/bash-defensive-patterns
Command: npx skills add https://github.com/legions-developer/invoicely --skill bash-defensive-patterns-legions-developer

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps you avoid brittle Bash scripts that fail silently, corrupt data, or break in production by teaching defensive programming techniques with strict mode, validation, and safe cleanup.

Core Features & Use Cases

  • Strict mode & safer execution: Uses recommended Bash flags (e.g., set -Eeuo pipefail) to catch errors early and prevent common scripting pitfalls.
  • Robust error trapping and cleanup: Defines reliable trap patterns for consistent error reporting and temporary resource cleanup.
  • Safer data handling and orchestration: Covers quoting, arrays, NUL-safe file iteration, idempotency patterns, dry-run support, structured logging, and signal-aware process shutdown.

Use Case: You maintain CI/CD deployment scripts that must be reliable across edge cases (missing vars, failing commands, partial state); you apply these patterns to reduce production incidents and make failures diagnosable.

Quick Start

Ask the assistant to generate a production-ready Bash script using strict mode, validated inputs, structured logging, safe temp files with cleanup traps, and a dry-run option for the requested operation.

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 bash scripts from failing silently in CI/CD pipelines?

Prevent silent bash script failures in CI/CD by enabling strict mode with `set -Eeuo pipefail` and implementing robust error traps. This catches unbound variables and pipe failures early, ensuring deployment scripts exit loudly on errors instead of corrupting state.

What is the best way to handle cleanup when a shell script fails unexpectedly?

Handle shell script cleanup during unexpected failures by defining reliable `trap` patterns for ERR and EXIT signals. This ensures temporary files are removed and resources are released consistently, making automation scripts safe for reruns without leaving partial state.

How do I safely iterate files with spaces in bash automation scripts?

Safely iterate files with spaces or special characters in bash automation scripts by applying comprehensive quoting, using arrays, and implementing NUL-safe file iteration. This defensive data handling prevents word splitting issues that corrupt file paths during system administration tasks.

Does this approach support dry-run options for deployment workflows?

Yes, this defensive scripting approach supports dry-run options for deployment workflows. It includes idempotency patterns and structured logging, allowing you to validate operations and handle edge cases like missing variables before applying changes in production environments.

Why do my bash scripts break when processing missing environment variables?

Bash scripts break on missing environment variables because standard execution does not enforce input validation. Applying strict mode and defensive programming validates arguments and variables upfront, stopping execution safely before an unbound variable causes data corruption.