All skills
wshobson avatar

/bash-defensive-patterns

@be57c0b
by Seth Hobsonwshobson/agents40k stars
4,281

Master defensive Bash programming techniques for production-grade scripts. Use when writing robust shell scripts, CI/CD pipelines, or system utilities requiring fault tolerance and safety.

Use this Skill: https://skilld.dev/gh/wshobson/agents/bash-defensive-patterns

This session only. Nothing lands on disk.

SKILL.md

≈53 tokens always: the name and description. ≈438 when used: this file. ≈2.4k more on demand in 1 file.

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

Detailed patterns and worked examples

Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.

Best Practices Summary

  1. Always use strict mode - set -Eeuo pipefail
  2. Quote all variables - "$variable" prevents word splitting
  3. Use [[]] conditionals - More robust than [ ]
  4. Implement error trapping - Catch and handle errors gracefully
  5. Validate all inputs - Check file existence, permissions, formats
  6. Use functions for reusability - Prefix with meaningful names
  7. Implement structured logging - Include timestamps and levels
  8. Support dry-run mode - Allow users to preview changes
  9. Handle temporary files safely - Use mktemp, cleanup with trap
  10. Design for idempotency - Scripts should be safe to rerun
  11. Document requirements - List dependencies and minimum versions
  12. Test error paths - Ensure error handling works correctly
  13. Use command -v - Safer than which for checking executables
  14. Prefer printf over echo - More predictable across systems

Source: SKILL.md on GitHub

1 warning16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    This skill provides educational guidance and templates for writing robust, production-grade Bash scripts using industry-standard defensive programming techniques. No security issues were detected.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    1/1 file flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at be57c0b. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 3 days ago.

Activeupdated 4 months ago
  • bash
  • shell
  • error-handling
  • ci-cd
  • scripting
  • automation
  • logging
  • production

README badge

README badge for wshobson/agents/bash-defensive-patterns

Teaches defensive Bash techniques including strict mode, error trapping, input validation, and structured logging for production shell scripts. Covers CI/CD pipelines, system utilities, and deployment automation where fault tolerance and cross-platform reliability are required.

Generated from the current SKILL.md.

Does this skill cover CI/CD pipeline scripts?
Yes. The skill includes patterns for building error-resilient deployment automation and CI/CD pipeline scripts that handle edge cases safely.
What is strict mode and why does this skill emphasize it?
Strict mode is `set -Eeuo pipefail`, which enables exit-on-error, undefined variable detection, and pipe failure handling. The skill treats it as a foundational best practice for production scripts.
Does this cover error handling and logging?
Yes. The skill includes error trapping patterns, structured logging with timestamps and levels, and guidance on implementing comprehensive monitoring in shell scripts.
Will this help with scripts that need to run on multiple platforms?
Yes. The skill addresses cross-platform compatibility, including recommendations like using `printf` over `echo` and `command -v` instead of `which` for more predictable behavior across systems.
Does the skill cover temporary file handling?
Yes. It includes patterns for safely creating temporary files with `mktemp` and cleaning them up using trap handlers.

Generated from the current SKILL.md. These answers refresh after source changes.