shell-scripting-patternslisted
Install: claude install-skill sardonyx0827/dotfiles
# Shell Scripting Patterns
Best practices for bash/zsh scripts, with specific patterns for Claude Code hooks and dotfiles maintenance on macOS.
## Script Header & Strict Mode
Every bash script starts with:
```bash
#!/bin/bash
set -euo pipefail
```
- `-e` exits on error, `-u` errors on undefined variables, `-o pipefail` makes a pipeline fail if any stage fails
- Why: without these, a failed `cd` or a typo'd variable silently continues and corrupts state downstream
When a non-zero exit is expected (grep finding nothing, optional commands), handle it explicitly instead of dropping strict mode:
```bash
# grep returns 1 on no-match — don't let -e kill the script
matches=$(grep -c "pattern" file || true)
if ! command -v terminal-notifier >/dev/null 2>&1; then
# fallback path
fi
```
Note: hooks in this repo intentionally use `set -e` without `-u`/`pipefail` when they must be fail-open (see Hook Patterns below). Choose deliberately, not by omission.
## Quoting
Quote every expansion unless you explicitly want word splitting:
```bash
# ❌ WRONG: breaks on spaces, runs glob expansion
rm $FILE_PATH
[ -f $FILE_PATH ] && cat $FILE_PATH
# ✅ CORRECT
rm "$FILE_PATH"
[[ -f "$FILE_PATH" ]] && cat "$FILE_PATH"
```
- Prefer `[[ ]]` over `[ ]` in bash: no word splitting inside, supports `=~` regex and `&&`/`||`
- Use `"$@"` (never `$@` or `$*`) to forward arguments
## Variables & Functions
```bash
# Defaults for optional values (plays well with set -u)
timeout="${3:-5}"
LOG_DIR="$