# 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.

 

**Licence** MIT

 

**Source** wshobson/agents

 [Download zip](<https://delaparra-services.nl/skills/bash-defensive-patterns/download>)   

# 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 ` 
1. **Quote all variables** - ` "$variable" ` prevents word splitting 
1. **Use \[\[\]\] conditionals** - More robust than \[ \] 
1. **Implement error trapping** - Catch and handle errors gracefully 
1. **Validate all inputs** - Check file existence, permissions, formats 
1. **Use functions for reusability** - Prefix with meaningful names 
1. **Implement structured logging** - Include timestamps and levels 
1. **Support dry-run mode** - Allow users to preview changes 
1. **Handle temporary files safely** - Use mktemp, cleanup with trap 
1. **Design for idempotency** - Scripts should be safe to rerun 
1. **Document requirements** - List dependencies and minimum versions 
1. **Test error paths** - Ensure error handling works correctly 
1. **Use ` command -v `** - Safer than ` which ` for checking executables 
1. **Prefer printf over echo** - More predictable across systems

 

## Files

 

- ` LICENSE ` 
- ` SKILL.md ` 
- ` SOURCE.md ` 
- ` references/details.md `

 

## Related terms

 

- [\> and \>\>](<https://delaparra-services.nl/knowledge-hub/and>) 
- [| (pipe)](<https://delaparra-services.nl/knowledge-hub/pipe>) 
- [cat](<https://delaparra-services.nl/knowledge-hub/cat>) 
- [cd](<https://delaparra-services.nl/knowledge-hub/cd>) 
- [chmod](<https://delaparra-services.nl/knowledge-hub/chmod>) 
- [chown](<https://delaparra-services.nl/knowledge-hub/chown>) 
- [cp](<https://delaparra-services.nl/knowledge-hub/cp>) 
- [echo](<https://delaparra-services.nl/knowledge-hub/echo>) 
- [env](<https://delaparra-services.nl/knowledge-hub/env-cli-commands>) 
- [export](<https://delaparra-services.nl/knowledge-hub/export>) 
- [grep](<https://delaparra-services.nl/knowledge-hub/grep>) 
- [head](<https://delaparra-services.nl/knowledge-hub/head>) 
- [kill](<https://delaparra-services.nl/knowledge-hub/kill>) 
- [less](<https://delaparra-services.nl/knowledge-hub/less>) 
- [ls](<https://delaparra-services.nl/knowledge-hub/ls>) 
- [man](<https://delaparra-services.nl/knowledge-hub/man>) 
- [mkdir](<https://delaparra-services.nl/knowledge-hub/mkdir>) 
- [mv](<https://delaparra-services.nl/knowledge-hub/mv>) 
- [ps](<https://delaparra-services.nl/knowledge-hub/ps>) 
- [pwd](<https://delaparra-services.nl/knowledge-hub/pwd>) 
- [rm](<https://delaparra-services.nl/knowledge-hub/rm>) 
- [source](<https://delaparra-services.nl/knowledge-hub/source>) 
- [sudo](<https://delaparra-services.nl/knowledge-hub/sudo>) 
- [tail](<https://delaparra-services.nl/knowledge-hub/tail>) 
- [tar](<https://delaparra-services.nl/knowledge-hub/tar>) 
- [top / htop](<https://delaparra-services.nl/knowledge-hub/top-htop>) 
- [which](<https://delaparra-services.nl/knowledge-hub/which>) 
- [zip / unzip](<https://delaparra-services.nl/knowledge-hub/zip-unzip>)

 

[Installing an agent skill in Claude Code, Codex, Cursor and Copilot](<https://delaparra-services.nl/manuals/een-skill-installeren-in-je-ai-assistent>)

[HTML version](https://delaparra-services.nl/skills/bash-defensive-patterns)
