bash-defensive-patterns

bash-defensive-patterns is a skill for Claude Code, Codex from Unique-Divine/jiyuu. It costs 36 tokens per session (3,142 once invoked), scanned C, a copy of bash-defensive-patterns, MIT.

A guide to defensive Bash programming: writing shell scripts that handle errors, unexpected input, cleanup, logging, and platform differences carefully. Bash is a command-line scripting language commonly used for automation.

In plain words
What is it for?
Use it to write production scripts, CI/CD pipelines, deployment automation, system utilities, and shell libraries that need error handling and safe edge-case behavior.
Why use it?
It helps prevent scripts from silently continuing after failures or behaving dangerously in unusual situations. The guidance is aimed at making automation more reliable and maintainable.

Skill for Claude CodeCodex

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/unique-divine/jiyuu/bash-defensive-patterns
Any agent
npx skills add Unique-Divine/jiyuu --skill bash-defensive-patterns
Clone the repo
git clone --depth 1 https://github.com/Unique-Divine/jiyuu

Made for: Claude Code, Codex.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for bash-defensive-patterns

README.md
[![agentmods](https://agentmods.dev/badge/skills/unique-divine/jiyuu/bash-defensive-patterns.svg)](https://agentmods.dev/skills/unique-divine/jiyuu/bash-defensive-patterns)
Your own site
<a href="https://agentmods.dev/skills/unique-divine/jiyuu/bash-defensive-patterns"><img src="https://agentmods.dev/badge/skills/unique-divine/jiyuu/bash-defensive-patterns.svg" alt="Measured on agentmods" height="20"></a>
Per session 36 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 3,142 The whole file, excluding the scripts and references it only reads on demand.
Security scan C 2 findings. Scan, not verified.
Origin 100% copy Near-identical to another mod in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00036 $0.03142
Opus 5 $0.00018 $0.01571
Sonnet 5 $0.00007 $0.00628
Haiku 4.5 $0.00004 $0.00314

Measured 4d ago against content hash f47bd7979a06, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade C, and why

bash-defensive-patterns scanned grade C with 2 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 4d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Recursive force deletehighDestructive command

rm -rf with a variable or a broad path is one typo away from removing the wrong tree.

trap 'echo "Cleaning up..."; rm -rf "$TMPDIR"' EXIT

Makes network callslowCapability

Not a fault in itself. Listed so you know the mod talks to something, and to what.

local -a required=("jq" "curl" "git")
Origin

This is a copy

100% identical to bash-defensive-patterns — 0 lines differ, which has more behind it and is treated as the original. This page carries a canonical link to it rather than competing with it.

ai-skills/bash-defensive-patterns/SKILL.md · 534 lines

How it starts

The opening of the file, as written. The whole thing — 534 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Bash Defensive Patterns

Comprehensive guidance for writing production-ready Bash scripts using defensive programming techniques, error handling, and safety best practices to prevent common pitfalls and ensure reliability.

When to Use This Skill

  • Writing production automation scripts
  • Building CI/CD pipeline scripts
  • Creating system administration utilities
  • Developing error-resilient deployment automation
  • Writing scripts that must handle edge cases safely
  • Building maintainable shell script libraries
  • Implementing comprehensive logging and monitoring
  • Creating scripts that must work across different platforms

Core Defensive Principles

1. Strict Mode

Enable bash strict mode at the start of every script to catch errors early.

#!/bin/bash
set -Eeuo pipefail  # Exit on error, unset variables, pipe failures

Key flags:

  • set -E: Inherit ERR trap in functions
  • set -e: Exit on any error (command returns non-zero)
  • set -u: Exit on undefined variable reference
  • set -o pipefail: Pipe fails if any command fails (not just last)

2. Error Trapping and Cleanup

Implement proper cleanup on script exit or error.

#!/bin/bash
set -Eeuo pipefail

trap 'echo "Error on line $LINENO"' ERR
trap 'echo "Cleaning up..."; rm -rf "$TMPDIR"' EXIT

TMPDIR=$(mktemp -d)
# Script code here

3. Variable Safety

Always quote variables to prevent word splitting and globbing issues.

# Wrong - unsafe
cp $source $dest

# Correct - safe
cp "$source" "$dest"

# Required variables - fail with message if unset
: "${REQUIRED_VAR:?REQUIRED_VAR is not set}"

4. Array Handling

Use arrays safely for complex data handling.

# Safe array iteration
declare -a items=("item 1" "item 2" "item 3")

for item in "${items[@]}"; do
    echo "Processing: $item"
done

# Reading output into array safely
mapfile -t lines < <(some_command)
readarray -t numbers < <(seq 1 10)

5. Conditional Safety

Use [[ ]] for Bash-specific features, [ ] for POSIX.

Read the full file on GitHub · 534 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 4d ago First seen · 534 lines · 36 tokens per session scan C f47bd7979a06

Subscribe to this mod's changes

bash-defensive-patterns is a skill published in the GitHub repository Unique-Divine/jiyuu (6 stars, last pushed 22d ago), licensed MIT. It adds 36 tokens to every session and 3,142 once invoked, about $0.0002 per session on Opus 5. A static security scan graded it C with 2 findings (recursive force delete, makes network calls). It is 100% identical to bash-defensive-patterns, differing in 0 lines, and is treated as a copy.

Related

Other skills, from other repositories

repomix

Pack and analyze codebases into AI-friendly single files using Repomix. Use when the user wants to explore repositories, analyze code structure, find patterns, check token counts, or prepare codebase context for AI analysis. Supports both local directories and remote GitHub repositories.

yamadashy/repomix · 58 tokens

dd-code-generation

Use pup CLI for immediate Datadog operations or generate code for integration into applications.

DataDog/pup · 16 tokens

drt-analyze

Analyze DRT cluster health for a given time range. Reconstructs the operations timeline, checks CockroachDB metrics (availability, latency, storage, changefeeds, jobs, goroutines, admission control, LSM, KV prober) and logs for anomalies, correlates findings with disruptive operations to distinguish expected…

cockroachdb/cockroach · 149 tokens

redux-to-swr

Migrate React components from Redux + Saga to SWR hooks. Use when converting data fetching from Redux store (reducers, sagas, selectors, connect HOC) to SWR-based hooks in CockroachDB DB Console or cluster-ui.

cockroachdb/cockroach · 53 tokens

prune-npm-overrides

Audit the pnpm overrides a repo already carries, remove the ones upstream has since fixed, and require a justification comment on every one that stays. The counterpart to fix-npm-vulnerability. Use when asked to check, clean up, or prune the overrides, or when an override's tracking issue comes up for review.

thunder-id/thunderid · 71 tokens

mma-investigator

Expert system for investigating MMA (Multi-Metric Allocator) behavior on CockroachDB clusters. Helps oncall engineers diagnose load imbalances, understand rebalancing decisions, and identify why MMA did or didn't act.

cockroachdb/cockroach · 47 tokens