bash-defensive-patterns

Apply defensive Bash patterns for strict mode, error handling, and variable safety.

Updated Aug 23, 2026
One-click install
npx skills add https://github.com/TrollHeap/dotfiles --skill bash-defensive-patterns-trollheap
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: bash-defensive-patterns
Source: https://github.com/TrollHeap/dotfiles/tree/main/ai-config/.claude/templates/bash-scripting/.claude/skills/bash-defensive-patterns
Command: npx skills add https://github.com/TrollHeap/dotfiles --skill bash-defensive-patterns-trollheap

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Bash scripting often suffers from subtle errors and unhandled edge cases. This Skill provides a set of defensive patterns to help you write safer, more maintainable Bash scripts that fail gracefully and are easier to debug.

Core Features & Use Cases

  • Strict mode, error trapping, and variable safety to prevent common scripting mistakes.
  • Safe handling of temporary files, arrays, and cross-platform compatibility considerations.
  • Use cases include automation scripts, system utilities, and CI/CD tasks where reliability matters.

Quick Start

Enable strict mode and apply the defensive patterns to a new Bash script you are developing.

Frequently Asked Questions about bash-defensive-patterns

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

FAQPage Schema
How do I make bash scripts fail safely on errors and undefined variables?

To make bash scripts fail safely, enable strict mode and apply defensive patterns like error trapping and variable safety. This enforces script execution to halt on errors and unbound variables, preventing silent failures.

What is the best way to handle temporary files in bash automation scripts?

The best way to handle temporary files in bash is by using robust defensive patterns for temporary file management. This ensures safe creation, tracking, and cleanup of temporary resources, preventing orphaned files and data corruption.

How do I design idempotent bash scripts for CI/CD tasks?

To design idempotent bash scripts for CI/CD tasks, apply idempotent design patterns alongside strict mode and robust logging. This ensures repeated script executions produce consistent, reliable results without unintended side effects.

Why does my bash script continue running after a command fails?

A bash script continues running after a command fails when strict mode and error trapping are not enforced. Implementing defensive patterns forces the script to exit immediately upon encountering unhandled errors or failures.

Can I use defensive bash patterns for cross-platform system utilities?

Yes, you can use defensive bash patterns for cross-platform system utilities. These patterns include cross-platform compatibility considerations and safe array handling, ensuring scripts run reliably across diverse environments.

What are the limitations of using strict mode in bash scripting?

Strict mode limitations in bash scripting can include abrupt terminations if error trapping is not carefully managed for expected non-zero exit codes. Defensive patterns help mitigate this by combining strict modes with robust logging and error handling.