---
title: "Bash Scripting Patterns"
description: "Bash scripting conventions for script structure, error handling, argument parsing, and security. Covers systemd integration, backup scripts, deployment automation, and monitoring."
type: context
domain: development
tags: [bash, scripting, systemd, automation, shell]
---

# Bash Scripting Patterns

## Script Structure
- Use proper shebang (`#!/bin/bash`)
- Set error handling (`set -euo pipefail`)
- Include help/usage functions
- Implement proper argument parsing

## Best Practices
- **Input validation** before processing
- **Logging and error messages** to stderr
- **Exit codes** for script status
- **Function-based organization** for reusability

## Common Patterns
- **Service management** with systemd integration
- **Backup scripts** with rotation and compression
- **Deployment automation** with rollback capabilities
- **System monitoring** with alerting

## Security Considerations
- Avoid hardcoded credentials
- Use proper file permissions
- Sanitize user inputs
- Quote variables to prevent word splitting

## Related Documentation
- Examples: `/examples/bash/service-management.md`
- Examples: `/examples/bash/backup-scripts.md`
- Examples: `/examples/bash/deployment-automation.md`
- Reference: `/reference/bash/troubleshooting.md`
- Reference: `/reference/bash/security.md`