---
name: patrones-bash-defensivos
description: Guia de referencia para escribir scripts Bash de produccion robustos y tolerantes a fallos. Cubre manejo de errores, validacion de entradas, logging estructurado e idempotencia para pipelines CI/CD y utilidades de sistema.
license: MIT
metadata:
  id: ca4bbf84
  slug: patrones-bash-defensivos
  titulo: "Patrones Defensivos en Bash"
  servicio: Automatizaciones
  categoria_recurso: Inspiracion-Formacion
  tipo: referencia
  nivel: avanzado
  idioma: es
  idioma_original: en
  acceso: gratis
  precio_eur: 0
  plataformas: [Linux, macOS, CI/CD, Bash]
  dependencias: []
  licencia: { spdx: MIT, redistribuible: true, uso_comercial: true }
  fuente:
    repo: wshobson/agents
    url: https://github.com/wshobson/agents/tree/main/plugins/shell-scripting/skills/bash-defensive-patterns
    commit: cc37bfd
    autor: wshobson
    nombre_original: bash-defensive-patterns
    duplicados_en: []
  seguridad: { veredicto: seguro, riesgo: bajo, escaneado: "2026-06-14", motor: "grep-estatico+auditor-llm" }
  ficha:
    que_hace: "Proporciona patrones y buenas practicas para escribir scripts Bash seguros y listos para produccion."
    como_lo_hace: "Documenta tecnicas defensivas como strict mode, manejo de trampas, validacion de entradas, logging y soporte dry-run con ejemplos practicos."
  content_hash: "ca4bbf847150ae76d72d015577cf714645672f3cd2e6a27b3c57d77068c0e09b"
  version: 1.0.0
---

# 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
