bash-defensive-patterns

Write defensive Bash scripts with strict mode and error trapping.

38.6k|4.1k|Updated Jul 24, 2025
One-click install
npx skills add https://github.com/wshobson/agents --skill bash-defensive-patterns-wshobson
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: bash-defensive-patterns
Source: https://github.com/wshobson/agents/tree/main/plugins/shell-scripting/skills/bash-defensive-patterns
Command: npx skills add https://github.com/wshobson/agents --skill bash-defensive-patterns-wshobson

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps you write more reliable and fault-tolerant Bash scripts by teaching and demonstrating defensive programming techniques.

Core Features & Use Cases

  • Error Prevention: Catches common scripting errors before they cause issues.
  • Robustness: Ensures scripts handle unexpected inputs and conditions gracefully.
  • Use Case: When developing a critical CI/CD pipeline script, use this Skill's guidance to implement strict mode, proper error trapping, and safe variable handling to prevent pipeline failures.

Quick Start

Apply strict mode and error trapping to your Bash script by adding set -Eeuo pipefail and trap 'echo "Error on line $LINENO"' ERR at the beginning.

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 robust Bash scripts that don't fail silently in CI/CD pipelines?

Robust Bash scripts prevent silent failures by applying strict mode with `set -Eeuo pipefail` and implementing error trapping using `trap 'echo "Error on line $LINENO"' ERR` to halt execution on errors.

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

Defensive programming in shell scripting is the practice of adding strict error handling, variable safety, and temporary file management to scripts. You need it for reliable automation, CI/CD pipelines, and system administration tasks.

How do I handle unexpected inputs and variable errors in Bash automation?

Handle unexpected inputs and variable errors in Bash automation by enabling strict mode to catch unset variables, applying safe array handling, and using structured logging to gracefully manage unexpected conditions.

What's the best way to manage temporary files and parse arguments in production shell scripts?

Manage temporary files and parse arguments in production shell scripts by implementing idempotent design patterns and dedicated argument parsing techniques, ensuring safe cleanup and predictable execution.

Can I use these Bash error handling techniques for process orchestration and system administration?

These Bash error handling techniques directly support process orchestration and system administration by providing structured logging and fault-tolerant design patterns specifically built for reliable automation and system tasks.