Master defensive Bash programming techniques for production-grade scripts. Use when writing robust shell scripts, CI/CD pipelines, or system utilities requiring fault tolerance and safety.
Collected from public GitHub. Every version is saved exactly as it was and never changes. Install it at an exact version with the mdr tool, and every file is checked before it reaches your agent. The main file is checked against the SHA-256 fingerprint recorded here, and the others against the fingerprints GitHub keeps for its source commit. The automatic checks below grade the latest version, and the same checks always give the same file the same grade.
What the file says
# Bash Defensive Patterns
Comprehensive guidance for writing production-ready Bash scripts using defensive programming techniques, error handling, and safety best practices to prevent common pitfalls and ensure reliability.
## When to Use This Skill
- Writing production automation scripts
- Building CI/CD pipeline scripts
- Creating system administration utilities
- Developing error-resilient deployment automation
- Writing scripts that must handle edge cases safely
- Building maintainable shell script libraries
- Implementing comprehensive logging and monitoring
- Creating scripts that must work across different platforms
## Core Defensive Principles
### 1. Strict Mode
Enable bash strict mode at the start of every script to catch errors early.
```bash
#!/bin/bash
set -Eeuo pipefail # Exit on error, unset variables, pipe failures
```
**Key flags:**
- `set -E`: Inherit ERR trap in functions
- `set -e`: Exit on any error (command returns non-zero)
- `set -u`: Exit on undefined variable reference
- `set -o pipefail`: Pipe fails if any command fails (not just last)
### 2. Error Trapping and Cleanup
Implement proper cleanup on script exit or error.
```bash
#!/bin/bash
…
The line at the top of this page pins the label git:20251120.288a379, which is what most people want. This one pins the sha256 fingerprint of the contents instead, so it survives a label being moved. Either way the exact version is written to mdr.lock, and mdr install downloads and checks those contents again, so it installs them exactly or stops with an error.
GET https://markdownregistry.com/api/v1/artifacts/art_ylh4ujo6dzwxm6co
GET https://markdownregistry.com/api/v1/resolve?ref=hermeticormus/libreuiux-claude-code/bash-defensive-patterns
GET https://markdownregistry.com/api/v1/blob/2d999e911d49903368e7a25e0ca2acfb0f6ccb799113b32c6917cf4e13f8798a
Your AI assistant does the legwork. You hear about the deals worth your word. Hand yours the standing instructions at modelranch.com and it joins the network that reads files like this one.
hermeticormus/libreuiux-claude-code · .grok/skills/verify-libreuiux/SKILL.md · Install the LibreUIUX-Claude-Code plugin pack the way a user does, into a clean Claude Code config and a clean Grok…
hermeticormus/libreuiux-claude-code · plugins/agent-orchestration/skills/ui-agent-patterns/SKILL.md · Patterns for delegating UI work to agents: one synthesis agent versus specialized agents, multi-agent pipelines for…
hermeticormus/libreuiux-claude-code · plugins/archetypal-alchemy/skills/archetypal-combinations/SKILL.md · Rules and worked examples for combining an archetype (structure and behavior) with a Major Arcana card (color and mood)…
hermeticormus/libreuiux-claude-code · plugins/archetypal-alchemy/skills/major-arcana/SKILL.md · The 22 Major Arcana cards mapped to color palettes, lighting, gradients, and moods for UI. Use when a design needs a…
hermeticormus/libreuiux-claude-code · plugins/backend-development/skills/api-design-principles/SKILL.md · Master REST and GraphQL API design principles to build intuitive, scalable, and maintainable APIs that delight…
hermeticormus/libreuiux-claude-code · plugins/backend-development/skills/temporal-python-testing/SKILL.md · Test Temporal workflows with pytest, time-skipping, and mocking strategies. Covers unit testing, integration testing…
hermeticormus/libreuiux-claude-code · plugins/blockchain-web3/skills/defi-protocol-templates/SKILL.md · Implement DeFi protocols with production-ready templates for staking, AMMs, governance, and lending systems. Use when…