bash-defensive-patterns

Implements defensive Bash programming patterns for fault-tolerant shell scripts and automation.

Updated Apr 23, 2026
One-click install
npx skills add https://github.com/SanketAdlak/PDMProjectDesign --skill bash-defensive-patterns-sanketadlak
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: bash-defensive-patterns
Source: https://github.com/SanketAdlak/PDMProjectDesign/tree/main/.agents/skills/bash-defensive-patterns
Command: npx skills add https://github.com/SanketAdlak/PDMProjectDesign --skill bash-defensive-patterns-sanketadlak

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Shell scripts often fail silently, mishandle edge cases, or cause destructive side effects in production environments. This Skill provides proven defensive programming patterns that make Bash scripts fail safely, handle errors predictably, and remain safe to rerun. ## Core Features & Use Cases - Strict Mode & Error Trapping: Enforces set -Eeuo pipefail with ERR/EXIT traps for early failure detection and guaranteed cleanup of temporary resources. - Safe Variable & File Handling: Covers quoting rules, array iteration, NUL-safe file processing, atomic writes, and safe move/delete operations. - Production Patterns: Includes robust argument parsing, structured logging, signal-aware process orchestration, dependency checking, dry-run support, and idempotent design. - Use Case: When writing a CI/CD deployment script that must clean up temp files on failure, validate required environment variables, and support a dry-run mode before touching production systems. ## Quick Start Write a production-grade Bash deployment script using strict mode, error trapping, argument parsing, and structured logging.

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 a safe Bash script with error handling?

Start every script with set -Eeuo pipefail to exit on errors, undefined variables, and pipe failures. Add trap handlers for ERR and EXIT to log failures and clean up temporary files, and quote all variable expansions to prevent word splitting.

What does set -euo pipefail do in Bash?

set -e exits the script when any command returns non-zero, set -u treats unset variables as errors, and set -o pipefail makes a pipeline fail if any command in it fails. Adding set -E ensures ERR traps are inherited inside functions.

Should I use [[ ]] or [ ] for Bash conditionals?

Use [[ ]] for Bash-specific scripts because it avoids word splitting and pathname expansion, and supports && and || natively. Use [ ] only when POSIX portability across different shells is required.

How do I parse command-line arguments in Bash safely?

Use a while loop with a case statement over $1, shifting after each option, and handle --help, unknown options, and the -- terminator explicitly. Validate required arguments after parsing and fail with a usage message when they are missing.

Why does my Bash script fail when filenames contain spaces?

Unquoted variables undergo word splitting, so paths with spaces break into multiple arguments. Always quote expansions like "$file" and use find with -print0 plus read -d '' for NUL-safe iteration over filenames.

How do I make a Bash script safe to rerun?

Design for idempotency by checking whether resources already exist before creating them, such as testing for directories with [[ -d ]] before mkdir. Combine this with dry-run support so users can preview changes before execution.