Core Standards
Every script must include these essential elements:1. Bash Shebang
All scripts must start with a bash shebang:Use
#!/usr/bin/env bash when you need bash 4.0+ features or want better portability across systems.2. Documentation Header
Include a comprehensive header at the top of every script:- Script name and brief description
- Usage syntax
- Available options
- Example commands
3. Error Handling
Useset -eo pipefail for robust error handling:
-e: Exit immediately if any command fails-o pipefail: Return exit code of first failed command in pipeline
Some interactive scripts may use
set -o pipefail without -e to handle errors more gracefully. This is acceptable when the script needs fine-grained error control.4. Help Flag
Every script must support the--help flag:
5. Executable Permissions
All scripts must be executable:Color Standards
Use consistent color coding for output messages:Color Usage Guidelines
| Color | Use Case | Function | Example |
|---|---|---|---|
| Red | Errors, failures | error() | error "File not found" |
| Green | Success, completion | success() | success "Upload complete" |
| Yellow | Warnings, cautions | warning() | warning "Large file size" |
| Blue | Info, progress | log() | log "Processing..." |
| Cyan | Headers, titles | Help text | ${CYAN}Script Name${NC} |
| Magenta | Special info | Custom | echo -e "${MAGENTA}Note${NC}" |
| Dim | Debug, comments | debug() | debug "Variable: $VAR" |
Standard Logging Functions
Implement these logging functions in every script:Important: Error messages should be written to stderr using
>&2 to allow proper output redirection.Argument Parsing
Use a consistent argument parsing pattern:Standard Options
These options should be supported by all scripts:-h, --help- Display help message-d, --debug- Enable debug mode (dry-run)-v, --verbose- Enable verbose output
Code Organization
Organize script sections in this order:Debug Mode
All scripts should support a debug mode that:- Shows what would be executed without making changes
- Prints debug messages
- Can be enabled with
--debugflag
File Structure Standards
Naming Conventions
- Use lowercase with hyphens:
backup-files.sh - Use underscores for multi-word scripts:
gdrive_ingest.sh - Use
.shextension for all bash scripts
Directory Organization
cloud/- Cloud storage servicessystem/- System utilities and automationtemplates/- Script templates
Configuration Files
For scripts that need configuration:- Use
.envfiles for sensitive data:
- Provide
.env.exampletemplate:
- Validate required variables:
Documentation Standards
Each script or project should have:- Inline comments for complex logic
- README.md for projects with multiple files
- Help text accessible via
--help - Usage examples in header and help text
Error Messages
Write clear, actionable error messages:- What went wrong
- What was expected
- How to fix it (when applicable)
Exit Codes
Use standard exit codes:0- Success1- General error2- Misuse of command (invalid arguments)
Best Practices
1. Validate Dependencies
2. Use Quotes
Always quote variables to prevent word splitting:3. Check File Existence
4. Cleanup on Exit
Use traps for cleanup:Examples from Real Scripts
gdrive_ingest.sh
Shows advanced features:- Configuration loading from
.env - Interactive UI with cursor control
- Trap handlers for cleanup
- Multiple output functions
power_guard.sh
Shows minimal script:- Simple utility script
- Basic structure without template complexity
- Direct implementation
Checklist
Before committing a script, verify:- Bash shebang present
- Documentation header complete
- Help flag implemented
- Error handling with
set -eo pipefail - Color-coded output
- Logging functions (log, success, error, warning, debug)
- Debug mode support
- Argument parsing with validation
- Executable permissions set
- Syntax validated with
bash -n - README or inline documentation
Next Steps
- Read Creating Scripts to build your first script
- Learn Testing approaches to validate your code
- Browse existing scripts for examples and patterns