bash-defensive-patterns_skill

This skill helps you write robust Bash scripts using strict mode, guarded error handling, and safe patterns for production reliability.
  • TypeScript

6

GitHub Stars

1

Bundled Files

2 months ago

Catalog Refreshed

4 months ago

First Indexed

Readme & install

Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.

Installation

Preview and clipboard use veilstrat where the catalogue uses aiagentskills.

npx veilstrat add skill gohypergiant/agent-skills --skill bash-defensive-patterns

  • SKILL.md11.5 KB

Overview

This skill teaches defensive Bash programming techniques for writing production-grade shell scripts. It focuses on strict mode, error trapping, safe file and temp handling, structured logging, idempotency, and robust argument parsing. Use it to make automation, CI/CD, and system utilities safer and more maintainable.

How this skill works

The skill provides concrete patterns and reusable functions that you drop into scripts: strict-mode bootstrap, trap-based cleanup, safe variable and array handling, atomic writes, dependency checks, and dry-run support. Each pattern explains what it protects against and includes minimal, copy-paste-ready code you can adapt. The goal is to reduce common failure modes and make scripts predictable under error conditions.

When to use it

  • Building production automation scripts and system utilities
  • Authoring CI/CD pipeline steps that must be robust and repeatable
  • Creating scripts that run unattended or on varied platforms
  • Handling files, temporary data, or background processes safely
  • Implementing idempotent deployment and configuration workflows

Best practices

  • Enable strict mode: set -Eeuo pipefail at script start
  • Quote all variable expansions and validate required vars early
  • Use trap for cleanup and ERR handling to avoid resource leaks
  • Prefer arrays, mapfile/readarray and NUL-safe iteration for lists
  • Provide dry-run and verbose flags to preview and diagnose changes
  • Check dependencies with command -v and fail fast with clear errors

Example use cases

  • A deployment script that must roll back or clean temporary files on failure
  • A CI job step that parses inputs, validates tools, and exits cleanly on problems
  • A system maintenance utility that runs periodically and must be idempotent
  • A multi-process orchestration script that needs graceful signal handling
  • A safe file-processing tool that writes atomically and avoids clobbering data

FAQ

It enables strict mode so the script exits on errors, treats unset variables as failures, and makes pipelines fail if any command fails, catching mistakes early.

How do I handle temporary files safely?

Create a dedicated temp directory with mktemp -d, trap its removal on EXIT, and write files inside before atomically moving them into place.

Built by
VeilStrat
AI signals for GTM teams
© 2026 VeilStrat. All rights reserved.All systems operational