bash-defensive-patterns

Write defensive Bash scripts with strict mode, error trapping, and safe file handling patterns.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Shell scripts often fail silently, corrupt data, or behave unpredictably in production due to unquoted variables, missing error handling, and unsafe file operations. This Skill provides proven defensive programming patterns that make Bash scripts fault-tolerant and safe for CI/CD pipelines and system automation. ## Core Features & Use Cases - Strict Mode & Error Trapping: Apply set -Eeuo pipefail, ERR/EXIT traps, and cleanup handlers to catch failures early and release resources safely. - Safe Scripting Patterns: Covers variable quoting, array handling, argument parsing, structured logging, temporary file management, atomic writes, and dry-run support. - Use Case: When building a deployment script that must run unattended in CI/CD, use these patterns to validate inputs, log every step with timestamps, clean up temp directories on failure, and support a dry-run mode before touching production files. ## Quick Start Ask the AI to write a production-grade Bash deployment script using defensive patterns with strict mode, logging, and dry-run support.

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 for production?▼

Start every script with set -Eeuo pipefail to exit on errors, undefined variables, and pipe failures. Add trap handlers for cleanup, quote all variables, validate inputs before use, and implement structured logging with timestamps.

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 pipelines fail if any command in the chain fails. Adding set -E ensures ERR traps are inherited by functions.

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

Use [[ ]] for Bash-specific scripts because it avoids word splitting and pathname expansion issues, making tests safer. Use [ ] only when POSIX portability across different shells is required.

How do I handle temporary files safely in shell scripts?▼

Create temporary directories with mktemp -d and register a trap on EXIT to remove them automatically. This guarantees cleanup even when the script fails or receives a termination signal.

Why does my Bash script fail when filenames contain spaces?▼

Unquoted variables cause word splitting on spaces, breaking file operations. Always quote variables like "$file" and use find with -print0 paired with read -d '' for NUL-safe iteration over filenames.

How do I add dry-run mode to a Bash script?▼

Wrap destructive commands in a run_cmd function that checks a DRY_RUN flag. When enabled, it prints the command instead of executing it, letting users preview changes before applying them.