bash-defensive-patterns

Write defensive Bash scripts with strict error handling and input validation.

Updated Dec 23, 2025
One-click install
npx skills add https://github.com/drgaciw/academic-compliance-hub-glm --skill bash-defensive-patterns-drgaciw
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: bash-defensive-patterns
Source: https://github.com/drgaciw/academic-compliance-hub-glm/tree/main/agents/plugins/shell-scripting/skills/bash-defensive-patterns
Command: npx skills add https://github.com/drgaciw/academic-compliance-hub-glm --skill bash-defensive-patterns-drgaciw

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill empowers you to write highly reliable and fault-tolerant Bash scripts, preventing common errors and ensuring your automation runs smoothly in production environments.

Core Features & Use Cases

  • Strict Mode Enforcement: Catches errors early with set -Eeuo pipefail.
  • Error Trapping & Cleanup: Ensures graceful exit and resource management.
  • Variable Safety: Prevents unexpected behavior with proper quoting and checks.
  • Use Case: When building CI/CD pipelines or critical system administration scripts, use this Skill's patterns to guarantee that your scripts handle unexpected conditions without failing catastrophically.

Quick Start

Write a Bash script that safely determines its own directory and exits if any command fails.

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 my Bash script exit automatically when a command fails?

To make a Bash script exit automatically on command failure, enable strict mode using `set -e`. This ensures your shell scripting environment catches errors early and terminates execution instead of continuing with a failed state.

What is defensive programming in shell scripting and when do I need it?

Defensive programming in shell scripting involves adding strict error checking, safe variable handling, and cleanup traps. You need it for automation scripts, CI/CD pipelines, and system utilities to ensure fault tolerance and prevent catastrophic failures in production.

How do I handle errors and clean up resources in a production Bash script?

Handle errors and clean up resources in a production Bash script by implementing error trapping with the `trap` command. This ensures graceful exits and proper resource management, automatically executing cleanup routines when your script encounters unexpected conditions.

What is the best way to prevent unbound variable errors in Bash automation scripts?

The best way to prevent unbound variable errors in Bash automation scripts is to enable strict mode with `set -u` and apply proper variable quoting. These defensive programming techniques prevent unexpected behavior when variables are undefined.

How do I safely determine a script's own directory in Bash?

Safely determine a script's own directory in Bash by using defensive programming patterns that combine strict error checking with reliable command substitution. This ensures your script correctly resolves its path without failing under unexpected environmental conditions.

Does pipefail work with set -e to catch errors in piped Bash commands?

Yes, `pipefail` works with `set -e` to catch errors in piped Bash commands. By enabling `set -Eeuo pipefail`, your script ensures that if any command in a pipeline fails, the entire pipeline returns a failure code, triggering an immediate script exit.