bash-defensive-patterns

Apply defensive programming patterns to Bash scripts for robust execution.

4|Updated Mar 3, 2026
One-click install
npx skills add https://github.com/AI-Foundry-Core/ril-agents --skill bash-defensive-patterns-ai-foundry-core
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: bash-defensive-patterns
Source: https://github.com/AI-Foundry-Core/ril-agents/tree/main/plugins/shell-scripting/skills/bash-defensive-patterns
Command: npx skills add https://github.com/AI-Foundry-Core/ril-agents --skill bash-defensive-patterns-ai-foundry-core

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Solves the problem of brittle Bash scripting by applying defensive programming techniques to ensure robust and predictable scripts.

Core Features & Use Cases

  • Strict mode: Enable Bash strict mode to fail fast on errors, unset variables, and pipe failures.
  • Error trapping and cleanup: Implement traps to perform cleanup and log failures, improving reliability.
  • Variable safety and quoting: Enforce safe variable usage with proper quoting and parameter checks.
  • Idempotent patterns: Make scripts safe to re-run without side effects, essential for automation.
  • Use case: When building CI/CD pipelines or system utilities that must operate reliably across environments.

Quick Start

Write a defensive Bash script that logs errors, enables strict mode, and safely handles temporary files.

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 fast on errors and unset variables?

To make bash scripts fail fast, enable strict mode using set options like -e, -u, and -o pipefail. This ensures scripts exit immediately on errors, undefined variables, and pipe failures, resulting in robust and predictable execution.

How do I safely handle temporary files in shell scripting?

Safely handle temporary files in shell scripting by implementing traps to perform cleanup operations when the script exits. This defensive pattern ensures temp files are removed reliably even if the script fails unexpectedly.

What are idempotent patterns for bash automation tasks?

Idempotent patterns in bash automation make scripts safe to re-run without causing side effects. This is essential for CI/CD pipelines and system utilities that must operate reliably across multiple environments and repeated executions.

Why do my bash scripts continue running after a command fails in a pipeline?

Bash scripts continue after pipeline failures because the default mode only checks the final command's exit status. Apply defensive programming by enabling strict mode with pipefail to trap failures and halt execution.

Do I need defensive programming for bash scripts in CI/CD pipelines?

Yes, defensive programming is critical for bash scripts in CI/CD pipelines. Applying strict mode, error trapping, variable safety, and idempotency ensures your automation tasks operate reliably across different environments.

What is the best way to enforce safe variable quoting in bash scripting?

The best way to enforce safe variable usage in bash scripting is through defensive programming techniques that mandate proper quoting and parameter checks. This prevents word splitting and globbing issues, ensuring script robustness.