bash-scriptslisted
Install: claude install-skill matthull/my-skills
# Bash Scripts Practice
## CRITICAL: Bash Script Safety (ABSOLUTE)
**You MUST ALWAYS start scripts with:**
```bash
#!/usr/bin/env bash
set -euo pipefail
```
- `set -e` - Exit immediately if any command fails
- `set -u` - Exit if undefined variable is used
- `set -o pipefail` - Catch failures in pipes
**EXCEPTION:** Only use `set +e` temporarily for commands you EXPECT to fail, then re-enable.
---
## CRITICAL: Test Every Bash Function (ABSOLUTE)
**You MUST NEVER add bash functions without bats tests.**
Every public function needs a `@test` annotation in bats:
```bash
# test/validator.bats
@test "validate_config accepts valid JSON" {
run validate_config valid.json
[ "$status" -eq 0 ]
}
@test "validate_config rejects invalid JSON" {
run validate_config invalid.json
[ "$status" -eq 1 ]
[[ "$output" =~ "Invalid JSON" ]]
}
```
---
## CRITICAL: Parameter Validation (ABSOLUTE)
Validate in every script:
- Required parameters exist
- Parameter count is correct
- File paths exist (if expected)
- Enums match expected values
```bash
if [ $# -lt 1 ]; then
echo "Error: Missing required parameter" >&2
echo "Usage: $0 <config_file>" >&2
exit 1
fi
```
---
## CRITICAL: Idempotency (ABSOLUTE)
**Scripts MUST be safe to run multiple times.**
```bash
# WRONG:
mkdir output/ # Fails on second run
rm config.old # Fails if file doesn't exist
# CORRECT:
mkdir -p output/
rm -f config.old
```
---
## Script Structure Pattern
```bash
#!/usr/bin/env bash
set -e