bash-defensive-patterns

Guide defensive Bash scripting with error handling and safety practices.

6|1|Updated Sep 23, 2023
One-click install
npx skills add https://github.com/Unique-Divine/jiyuu --skill bash-defensive-patterns-unique-divine
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: bash-defensive-patterns
Source: https://github.com/Unique-Divine/jiyuu/tree/main/ai-skills/bash-defensive-patterns
Command: npx skills add https://github.com/Unique-Divine/jiyuu --skill bash-defensive-patterns-unique-divine

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

This Skill unit helps developers write robust and reliable Bash scripts by applying defensive programming techniques and safety best practices.

Core Features & Use Cases

  • Error Handling: Implement error trapping, strict mode, and proper cleanup to catch and handle errors effectively.
  • Variable Safety: Safely handle variables with strict quoting and avoid common pitfalls like word splitting and globbing.
  • Array Handling: Safely manage arrays and complex data structures.
  • Conditional Safety: Use Bash-specific and POSIX conditionals to ensure script portability.
  • Script Directory Detection: Accurately determine the script's location and name.
  • Function Templates: Provide templates for well-structured functions with documentation and error handling.
  • Temporary File Handling: Safely create and manage temporary files.
  • Argument Parsing: Parse arguments robustly with validation and default values.
  • Structured Logging: Implement logging with timestamps and different severity levels.
  • Process Orchestration: Manage background processes and signal handling.
  • Safe File Operations: Safely move, delete, and write to files.
  • Idempotent Script Design: Ensure scripts are safe to run multiple times.
  • Safe Command Substitution: Use safe command substitution for external commands.
  • Dry-Run Support: Allow users to preview changes without making real changes.
  • Named Parameters Pattern: Use named parameters for better function arguments.
  • Dependency Checking: Check for required system commands.
  • Best Practices Summary: Summarize key best practices for defensive Bash programming.

Quick Start

Run the skill to learn about defensive Bash programming techniques and apply them to your scripts.

Frequently Asked Questions about bash-defensive-patterns

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

FAQPage Schema
How do I implement error handling in bash scripts to prevent silent failures?

Error handling in bash scripts uses error trapping, strict mode, and proper cleanup to catch and manage failures. This ensures robust script design by executing cleanup routines automatically when errors occur.

What is the best way to safely handle variables and avoid word splitting in bash?

Safely handle variables in bash by applying strict quoting practices to prevent word splitting and globbing. This approach protects variable integrity and avoids common pitfalls during script execution.

How do I make bash scripts idempotent and safe to run multiple times?

Idempotent bash scripts are designed to execute safely multiple times without causing duplicate side effects. This is achieved through robust argument parsing, safe file operations, and conditional checks.

Can I add dry-run support to bash scripts to preview changes before execution?

Dry-run support in bash scripts allows users to preview changes without making real modifications. Implementing this requires structured logging and safe command substitution to simulate operations.

How do I parse arguments robustly in bash with validation and default values?

Robust argument parsing in bash validates inputs and assigns default values using named parameters patterns. This ensures scripts execute predictably even when required system commands or arguments are missing.