Rules: shell
A script that passes on Linux (bash 5, GNU tools) can fail only on macOS.
Affected files
These rules apply to files matching the following patterns:
**/*.sh**/*.bash**/*.bats
Detailed rules
Shell Rules
Strict mode
- Start every executable script with
#!/usr/bin/env bashandset -euo pipefail - Do NOT set it in a library meant to be sourced: it would change the caller's shell
- Know its gaps — they fail silently:
set -eis ignored insideif,while,&&/||lists and functions called from themlocal x=$(cmd)returns the status oflocal, not ofcmd: declare first, assign on a second line- without
pipefail, a pipe's status is its LAST stage's
# Correct: the failure of cmd is not masked
local out
out=$(cmd)
Quoting and expansion
- IMPORTANT: quote every expansion:
"$var","$@","${arr[@]}" - Guard a variable used in a destructive path:
rm -rf -- "${dir:?}/build" - Put
--before arguments that may start with a dash - Use arrays, not strings, to build a command line
- Prefer
[[ ]]over[ ]in bash;(( ))for arithmetic
Portability (macOS ships bash 3.2)
A script that passes on Linux (bash 5, GNU tools) can fail only on macOS.
| Avoid on bash 3.2 | Use instead |
|---|---|
declare -A (associative arrays) | parallel arrays, or a case |
mapfile / readarray | while IFS= read -r line; do …; done |
${x,,} / ${x^^} | tr '[:upper:]' '[:lower:]' |
"${arr[@]}" on an empty array under set -u | ${arr[@]+"${arr[@]}"} |
local -n (nameref), ${x@Q} | pass names explicitly, printf %q |
GNU and BSD tools differ too: sed -i (BSD needs -i ''), readlink -f, date -d, grep -P.
Prefer POSIX options, or detect the variant.
Errors and cleanup
- Error messages go to stderr (
>&2), with a non-zero exit code - Temporary files:
mktemp, removed bytrap 'rm -rf -- "$tmp"' EXIT cd dir || exit 1— never a barecd- Guard an optional tool:
command -v tool >/dev/null 2>&1 || { echo "tool missing" >&2; exit 1; }
shellcheck
- IMPORTANT: run
shellcheckon every script before committing - Fix the finding; disable one only inline, with the reason:
# shellcheck disable=SC2034 # read by the caller
Tests (bats)
run cmd, then assert$statusand$output- IMPORTANT: a
[[ ]]or! cmdthat is NOT the last line of a test does not fail it on every bash: end it with|| false - Make a script testable by sourcing:
[[ "${BASH_SOURCE[0]}" == "$0" ]] && main "$@" - Stub external commands with a fake on
PATH, never the real network or service - Each test gets its own temp dir, created in
setup, removed inteardown - A test that asserts an ABSENCE needs a control that proves the presence is detectable
Anti-patterns
- Parsing
lsoutput (use globs orfind -print0) evalon anything derived from inputcurl … | sh(download, verify, then run)- Unquoted
$varinrm,cp,mv,[ ] cat file | grepand other useless pipes in hot loops
Automatic application
These rules are automatically applied by Claude during:
- Reading the matching files
- Modifying code
- Suggestions and fixes