Shell Guru
Enforce modern, safe, idiomatic Bash 5+ across every task. Never suggest unquoted variables, unsafe patterns, or legacy syntax. Default to Bash unless POSIX portability is explicitly required. Flag bashisms when writing portable POSIX sh.
When in doubt, check the reference files before answering.
When to Use
- Writing new shell scripts of any kind
- Reviewing or refactoring existing Bash/sh scripts
- Setting up shell script projects with linting and CI
- Debugging scripts, unexpected word splitting, or pipefail surprises
- Advising on error handling, traps, portability, or security
- User asks: "is this idiomatic bash?", "how do I do X in shell?", "review my script", "why does my script fail silently?"
When NOT to Use
- Pure interactive shell config (
.zshrc, aliases, prompts): this skill covers scripts only - Other languages where shell is incidental (e.g. Python
subprocesscalls) - System administration without script authorship
Enforcement Checklist
Run through this for every shell script you write or review.
Script Header
- ☐ Shebang is
#!/usr/bin/env bash(or#!/bin/shfor POSIX-only targets) - ☐
set -Eeuo pipefailon the first non-comment line after the shebang - ☐
shopt -s inherit_errexitadded for Bash 4.4+ (subshells inheriterrexit) - ☐
readonlyused for all script-level constants - ☐ Script is executable:
chmod +x script
Variables & Quoting
- ☐ All variable expansions double-quoted:
"$var","${var}","$(cmd)" - ☐ Arrays expanded with
"${arr[@]}". Never bare${arr[*]}(word splits) - ☐ Default values with
${var:-default}; mandatory vars with${var:?error} - ☐
declare -rfor readonly,-iinteger,-aarray,-Aassociative map - ☐
localdeclared for every variable inside a function - ☐
local x; x=$(cmd). Neverlocal x=$(cmd)(masks the exit code!) - ☐
read -ralways (prevents backslash interpretation) - ☐
$()for command substitution; backticks are obsolete
Conditionals & Loops
- ☐
[[ ]]for all conditionals in Bash (not[ ]ortest) - ☐
(( ))for integer arithmetic tests:(( count > 0 )) - ☐
==/!=inside[[ ]];=inside POSIX[ ] - ☐ Loops over files:
for f in dir/*orwhile IFS= read -r line - ☐ No iterating
$(ls ...). Use globs orfind
Error Handling & Traps
- ☐
trap 'cleanup' EXIT INT TERMfor temp-file and state cleanup - ☐
trap 'on_error "$LINENO"' ERRfor actionable failure messages - ☐ Exit code preserved in cleanup:
local _exit=$?; ...; exit "$_exit" - ☐
cdresult always checked:cd /some/dir || exit 1 - ☐ Non-critical teardown steps use
|| true
File & Process Safety
- ☐ Temp files created with
tmpfile=$(mktemp). Never a hardcoded/tmp/name - ☐
mapfile -t arr < fileorwhile IFS= read -r lineto read files line-by-line - ☐
findor globs used instead of parsinglsoutput - ☐
--added before user-supplied arguments passed to commands - ☐
printf '%s\n' "$var"preferred overecho "$var"for non-literal output
Security
- ☐ No
evalon external or user-supplied data - ☐ No
bash -c "$user_input"(command injection) - ☐ All file paths from external sources validated before use
- ☐ No hardcoded credentials. Use env vars or permission-restricted files
- ☐ Scripts run with least privilege;
sudoscope minimised
Tooling
- ☐ Script passes
shellcheck -x -s bash scriptwith zero warnings - ☐ Formatted with
shfmt -i 2 -ci -bn -sr - ☐ Any disabled shellcheck rules have an inline comment explaining why
- ☐ Non-trivial logic has
bats-coretests - ☐
mise.tomltasks defined forlint,format, andtest
Quick-Reference: Old → New
| Old (avoid) | New (use) | ||
|---|---|---|---|
` cmd ` | $(cmd) | ||
[ "$x" = "y" ] | [[ "$x" == "y" ]] | ||
for f in $(ls dir/) | for f in dir/* | ||
if [ $? -eq 0 ] | if cmd; then | ||
| `cat file \ | cmd` | cmd < file (UUOC) | |
local x=$(cmd) | local x; x=$(cmd) | ||
/tmp/hardcoded-tmp | tmpfile=$(mktemp) | ||
read line | read -r line | ||
echo "$var" | printf '%s\n' "$var" | ||
set -e alone | set -Eeuo pipefail + shopt -s inherit_errexit | ||
[ -z $var ] | [[ -z "$var" ]] | ||
(( count++ )) near set -e | `(( count++ )) \ | \ | true`. See safety.md |
export VAR=val in functions | declare -g VAR=val (or just VAR=val if already global) |
Script Bootstrap
For a new Bash script, always start with this header:
#!/usr/bin/env bash
set -Eeuo pipefail
shopt -s inherit_errexit
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPT_DIR
# --- Logging ---
err() { printf '[ERROR] %s\n' "$*" >&2; }
info() { printf '[INFO] %s\n' "$*"; }
warn() { printf '[WARN] %s\n' "$*" >&2; }
# --- Cleanup ---
cleanup() {
local _exit=$?
[[ -f "${tmpfile:-}" ]] && rm -f "$tmpfile"
exit "$_exit"
}
on_error() { err "Unexpected failure at line $1 — command: ${BASH_COMMAND}"; }
trap cleanup EXIT INT TERM
trap 'on_error "$LINENO"' ERR
For a script project, use this layout:
my-script-project/
├── bin/
│ └── my-script # main entry point (no .sh extension)
├── lib/
│ └── helpers.bash # sourced library functions
├── tests/
│ ├── test_helper.bash
│ └── test_my_script.bats
├── .shellcheckrc
└── mise.toml
Minimal .shellcheckrc:
shell=bash
enable=all
source-path=lib
Minimal mise.toml:
[tools]
shellcheck = "latest"
shfmt = "latest"
bats = "latest"
[tasks.lint]
run = "shellcheck -x bin/* lib/*.bash"
[tasks.format]
run = "shfmt -i 2 -ci -bn -sr -w bin/* lib/*.bash"
[tasks."format:check"]
run = "shfmt -i 2 -ci -bn -sr -d bin/* lib/*.bash"
[tasks.test]
run = "bats tests/"
[tasks.check]
depends = ["lint", "format:check", "test"]
description = "Full pre-push check"
References
Load these files when you need depth beyond this overview:
references/tooling.md: shellcheck, shfmt, bats-core, mise, CI/CD setupreferences/safety.md: set/shopt options, traps, variable scoping, IFS, getoptsreferences/security.md: injection prevention, eval risks, temp file safety, secretsreferences/testing.md: bats-core test patterns, mocking, fixtures, CI integrationreferences/anti-patterns.md: full anti-pattern catalogue with fixes