| version | 1.0.0 |
| name | shell-script-quality |
| description | Write safe, portable shell scripts. Activate for bash/sh authoring, ShellCheck fixes, BATS test writing, and shell security patterns.
|
| category | quality |
| allowed-tools | Read Write Edit Grep Glob |
| license | MIT |
Shell Script Quality
Write safe, portable shell scripts. Focus on patterns, pitfalls, and security
—not on running checks (the quality gate already handles that).
Quick Start
- Lint with ShellCheck
- Fix reported issues
- Write BATS tests
- Verify tests pass
Core Workflow
Lint with ShellCheck
shellcheck script.sh
find scripts -name "*.sh" -exec shellcheck {} +
Common Fixes
- SC2086: Quote variables:
"$var" not $var
- SC2155: Separate declaration and assignment
- SC2181: Check exit code directly with
if ! command
Write BATS Tests
#!/usr/bin/env bats
setup() {
source "$BATS_TEST_DIRNAME/../scripts/example.sh"
}
@test "function succeeds with valid input" {
run example_function "test"
[ "$status" -eq 0 ]
[ -n "$output" ]
}
Run Tests
bats tests/
Script Template
#!/bin/bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
error_exit() {
echo "ERROR: $1" >&2
exit "${2:-1}"
}
main() {
[[ $# -lt 1 ]] && {
echo "Usage: $0 <argument>" >&2
exit 1
}
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi
Configuration
.shellcheckrc in project root:
shell=bash
disable=SC1090
enable=all
source-path=SCRIPTDIR
Validation Loop Pattern
- Make changes to script
- Validate immediately:
shellcheck script.sh
- If validation fails: fix and retry
- Only proceed when validation passes
- Run tests:
bats tests/script.bats