# Bash Defensive Patterns > 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. Source: https://skillsagentes.com/skills/wshobson/agents/bash-defensive-patterns Repository: https://github.com/wshobson/agents Author: wshobson License: MIT Updated: hace 2 meses Context cost: 47 tok installed, 498 tok once triggered, 2.9k tok with every bundled file Bundle: 2 files, 11 KB Permissions requested: none declared ## Install ```bash npx -y skills add wshobson/agents --skill bash-defensive-patterns --agent claude-code ``` ## What it does - Aplica técnicas de programación defensiva en Bash: strict mode, comillas en variables, [[]], trampas de error - Añade manejo de errores robusto, validación de entradas y limpieza segura de archivos temporales con mktemp/trap - Incorpora logging estructurado, modo dry-run y diseño idempotente en los scripts - Remite a `references/details.md` para patrones detallados y ejemplos trabajados ## Use it when - Escribir scripts de automatización de producción - Construir scripts de pipelines CI/CD - Crear utilidades de administración de sistemas - Desarrollar scripts que deben manejar casos límite de forma segura ## What triggers it - "Ayúdame a escribir un script de Bash robusto para mi pipeline de CI/CD" - "Necesito que este script de despliegue maneje errores correctamente" - "Revisa mi script de administración de sistema para hacerlo más seguro" ## Files - SKILL.md — 2 KB - references/details.md — 10 KB ## SKILL.md Reproduced verbatim from wshobson/agents under MIT. This section is the upstream document and is in English. # 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 --- Skills Agentes — https://skillsagentes.com/skills/wshobson/agents/bash-defensive-patterns