← ClaudeAtlas

ia-linux-bash-scriptinglisted

Defensive Bash scripting for Linux: safe foundations, argument parsing, production patterns, ShellCheck compliance. Use when writing bash scripts, shell scripts, cron jobs, or CLI tools in bash.
iliaal/whetstone · ★ 20 · DevOps & Infrastructure · score 84
Install: claude install-skill iliaal/whetstone
# Linux Bash Scripting Produce bash scripts that pass `shellcheck --enable=all` and `shfmt -d` with zero warnings. Target: GNU Bash 4.4+ on Linux. No macOS/BSD workarounds, no Windows paths, no POSIX-only restrictions. ## Script Foundation ```bash #!/usr/bin/env bash set -Eeuo pipefail shopt -s inherit_errexit readonly SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)" trap 'printf "Error at %s:%d\n" "${BASH_SOURCE[0]}" "$LINENO" >&2' ERR trap 'rm -rf -- "${_tmpdir:-}"' EXIT ``` - `-E` propagates ERR traps into functions - `inherit_errexit` propagates errexit into `$()` command substitutions - Always create temp dirs under the EXIT trap: `_tmpdir=$(mktemp -d)` - Wrap body in `main() { ... }` with source guard: `[[ "${BASH_SOURCE[0]}" == "$0" ]] && main "$@"` -- enables sourcing for testing ## Core Rules - Quote every expansion: `"$var"`, `"$(cmd)"`, `"${array[@]}"` - `local` for function variables, `local -r` for function constants, `readonly` for script constants - `printf '%s\n'` over `echo` -- predictable behavior, no flag interpretation - `[[ ]]` for conditionals; `(( ))` for arithmetic; `$()` over backticks - End options with `--`: `rm -rf -- "$path"`, `grep -- "$pattern" "$file"` - Require env vars: `: "${VAR:?must be set}"` - Never `eval` user input; build commands as arrays: `cmd=("grep" "--" "$pat" "$f"); "${cmd[@]}"` - Separate `local` from assignment to preserve exit codes: `local val; val=$(cmd)` - Debug tracing: `PS4='+${BASH_SOURCE[0]}