sh-style-guide
SH Style Guide
Scope Boundaries
- Use this skill when the task matches the trigger condition described in
description. - Do not use this skill when the primary task falls outside this skill's domain.
Use this skill to write and review POSIX sh scripts that run reliably across environments where Bash features are unavailable.
Trigger And Co-activation Reference
- If available, use
references/trigger-matrix.mdfor canonical co-activation rules. - If available, resolve style-guide activation from changed files with
python3 scripts/resolve_style_guides.py <changed-path>.... - If available, validate trigger matrix consistency with
python3 scripts/validate_trigger_matrix_sync.py.
Quality Gate Command Reference
- If available, use
references/quality-gate-command-matrix.mdfor CI check-only and local autofix mapping.
Quick Start Snippets
Portable script skeleton
#!/bin/sh
set -eu
SCRIPT_NAME=$(basename "$0")
TEMP_DIR=$(mktemp -d)
cleanup() {
rm -rf -- "$TEMP_DIR"
}
on_error() {
line_number="$1"
echo "$SCRIPT_NAME: failed at line $line_number" >&2
}
trap cleanup EXIT HUP INT TERM
trap 'on_error "$LINENO"' ERR
main() {
echo "temp dir: $TEMP_DIR"
}
main "$@"
Required environment variable check (no silent default)
: "${API_TOKEN:?API_TOKEN is required}"
: "${API_BASE_URL:?API_BASE_URL is required}"
POSIX-safe argument handling (no arrays)
run_curl() {
url="$1"
curl --fail --silent --show-error \
--header "Authorization: Bearer ${API_TOKEN}" \
"$url"
}
Bounded retry loop
MAX_ATTEMPTS=5
RETRY_DELAY_SECONDS=2
retry_command() {
attempt=1
while [ "$attempt" -le "$MAX_ATTEMPTS" ]; do
if "$@"; then
return 0
fi
if [ "$attempt" -eq "$MAX_ATTEMPTS" ]; then
echo "command failed after $MAX_ATTEMPTS attempts" >&2
return 1
fi
sleep "$RETRY_DELAY_SECONDS"
attempt=$((attempt + 1))
done
}
Safe line reading
while IFS= read -r line; do
printf 'line=%s\n' "$line"
done < "$input_file"
Portability And Readability
- Target POSIX syntax only; avoid Bash/Zsh-specific features.
- Use
set -eufor executable scripts. - Prefer small functions and clear
mainorchestration. - Use uppercase constants and lowercase local variables.
- Keep comments short and intent-focused.
Data Handling And Quoting
- Quote parameter expansion by default.
- Avoid
evalunless strictly required and heavily validated. - Replace magic numbers with named constants and explicit units.
- Use
--for destructive command path arguments. - Validate all external input before using it in commands.
Error Handling And Safety
- Return explicit non-zero status for expected failure modes.
- Use
trapfor cleanup and signal handling. - Handle failures intentionally; do not mask with
|| trueunless justified. - Fail startup when required configuration is missing.
- Let failures surface when root-cause fixing is required.
Testing And Verification
- Add shell tests (
shunit2or project equivalent) for core paths. - Cover edge cases: empty input, whitespace paths, missing env vars, timeout/retry exhaustion.
- Document manual verification for environment-dependent behavior.
- Verify idempotency for repeatable automation scripts.
CI Required Quality Gates (check-only)
- Run
shellcheck -s sh. - Run
shfmt -d -ln posix. - Run shell tests (
shunit2/project equivalent). - Reject hidden failure paths and implicit behavior.
Optional Autofix Commands (local)
- Run
shfmt -w -ln posix. - Apply safe lint fixes, then rerun check-only commands.
More from kentoshimizu/sw-agent-skills
graph-algorithms
Graph algorithm workflow for modeling entities/relations and selecting traversal, path, ordering, or flow strategies. Use when correctness or performance depends on graph representation and algorithm choice; do not use for schema-only modeling or deployment topology planning.
14bash-style-guide
Style, review, and refactoring standards for Bash shell scripting. Trigger when `.sh` files, files with `#!/usr/bin/env bash` or `#!/bin/bash`, or CI workflow blocks with `shell: bash` are created, modified, or reviewed and Bash-specific quality controls (quoting safety, error handling, portability, readability) must be enforced. Do not use for generic POSIX `sh`, PowerShell, or language-specific application style rules. In multi-language pull requests, run together with other applicable `*-style-guide` skills.
11architecture-clean-architecture
Clean Architecture workflow for enforcing dependency direction, stable domain boundaries, and use-case-centered application design. Use when teams must separate business rules from frameworks and delivery mechanisms; do not use for isolated module cleanup without boundary implications.
11powershell-style-guide
Style, review, and refactoring standards for PowerShell scripting. Trigger when `.ps1`, `.psm1`, `.psd1` files, or CI workflow blocks with `shell: pwsh` or `shell: powershell` are created, modified, or reviewed and PowerShell-specific quality controls (error handling, parameter validation, readability, operational safety) must be enforced. Do not use for Bash, generic POSIX `sh`, or language-specific application style rules. In multi-language pull requests, run together with other applicable `*-style-guide` skills.
10github-codeowners-management
Govern CODEOWNERS rules so review routing reflects real ownership and risk boundaries on GitHub. Use when repository ownership mapping or mandatory reviewer rules must be defined, updated, or audited; do not use for non-GitHub runtime architecture or data-layer design.
9security-authentication
Security workflow for authentication architecture, credential lifecycle, and session/token assurance. Use when login, identity proofing, MFA, or session security decisions are required; do not use for authorization policy design or non-security quality tuning.
9