{"slug":"sota-shell-scripting","title":"sota-shell-scripting","summary":"State-of-the-art shell scripting (bash-focused, defensive) for writing and auditing shell scripts, CI scripts, init/deploy scripts, container entrypoints, and Makefile recipes — and, just as much, for the ad-hoc commands you run yourself: a grep/find/rg sweep whose result you are","platform":"Claude","tags":[],"authorName":"LLM Mart","authorSlug":"llm-mart","score":0,"source":"github","price":null,"verified":false,"createdAt":"2026-09-09T18:38:50.891928Z","repo":{"url":"https://github.com/martinholovsky/SOTA-skills","stars":28,"forks":3,"license":"CC-BY-4.0","updatedAt":"2026-11-10T07:08:54Z"},"bodyHtml":"<hr>\n<h2>name: sota-shell-scripting\ndescription: &gt;-\nState-of-the-art shell scripting (bash-focused, defensive) for writing and auditing shell scripts, CI scripts, init/deploy scripts, container entrypoints, and Makefile recipes — and, just as much, for the ad-hoc commands you run yourself: a grep/find/rg sweep whose result you are about to report, a one-liner pasted from a checklist, a pipeline whose exit status or empty output you are about to believe. Use when creating, modifying, reviewing or hardening shell code, AND before trusting any conclusion a shell command produced — especially an ABSENCE (\"no matches\", \"nothing found\", \"0 results\"), which a quoting bug produces identically. Trigger keywords: bash, shell script, sh, zsh, shellcheck, shfmt, CI script, Makefile shell, entrypoint script, set -euo pipefail, dotfiles, install script, cron job, wrapper script, one-liner, command line, grep sweep, search the codebase, verify a claim, no matches found, empty output, exit status, word splitting, glob.</h2>\n<h1>SOTA Shell Scripting</h1>\n<p>Purpose: produce shell scripts that survive contact with reality — unusual filenames, missing\ncommands, partial failures, hostile input, signals, and concurrent invocation — and audit\nexisting scripts for the defect classes that cause most production shell incidents:\nunquoted expansions, silent error swallowing, injection, secret leakage, and temp-file races.</p>\n<p>Bash-focused (bash 5.x current; macOS ships bash 3.2 and defaults to zsh — see portability\nrules). POSIX <code>sh</code> only when the target demands it (busybox/dash containers, init systems).</p>\n<h2>First decision: should this be shell at all?</h2>\n<p><strong>Do NOT use shell when any of these hold.</strong> Recommend Python/Go (or the project's primary\nlanguage) instead, and say so explicitly in BUILD and AUDIT output:</p>\n<ul>\n<li>Script exceeds ~100 lines of actual logic (not counting boilerplate/usage text).</li>\n<li>Needs real data structures (nested maps, JSON manipulation beyond a <code>jq</code> one-liner, sets).</li>\n<li>Needs granular error handling (retry <em>this</em> step, distinguish error kinds, partial rollback).</li>\n<li>Does arithmetic beyond integers, date math, or float comparison.</li>\n<li>Parses structured formats (JSON/YAML/XML) with string surgery instead of <code>jq</code>/<code>yq</code>.</li>\n<li>Needs portable concurrency beyond \"run N jobs and <code>wait</code>\".</li>\n<li>Is security-critical input handling (auth, parsing untrusted network data).</li>\n</ul>\n<p>Shell is the right tool for: gluing processes together, CI steps, container entrypoints,\nsmall install/deploy wrappers, environment setup — anything that is mostly <em>invoking other\nprograms</em> rather than computing.</p>\n<h2>BUILD mode</h2>\n<p>When writing or modifying shell scripts:</p>\n<ol>\n<li><strong>Pick the dialect deliberately.</strong> <code>#!/usr/bin/env bash</code> unless the target is a minimal\ncontainer/init context that only guarantees POSIX <code>sh</code>. Never <code>#!/bin/sh</code> with bashisms.</li>\n<li><strong>Start every bash script from the safety preamble</strong> (rules/01): <code>set -euo pipefail</code>,\ntrap-based cleanup, <code>IFS</code> discipline — and know where <code>set -e</code> does NOT fire.</li>\n<li><strong>Quote every expansion.</strong> <code>\"$var\"</code>, <code>\"$@\"</code>, <code>\"$(cmd)\"</code>. Build commands with arrays,\nnever with string concatenation.</li>\n<li><strong>Errors are loud and routed to stderr</strong> with script name + context; exit codes are\nmeaningful and documented in <code>--help</code>.</li>\n<li><strong>Make it idempotent and interrupt-safe</strong>: <code>mktemp</code> + <code>trap</code> cleanup, atomic writes via\n<code>mv</code>, check-before-create, <code>flock</code> if concurrent runs are possible.</li>\n<li><strong>Run <code>shellcheck</code> (treat all findings as blockers, suppress only with a justifying\ncomment) and <code>shfmt -d</code> before declaring done.</strong> If they are unavailable locally, state\nthat and flag CI must run them.</li>\n<li>Provide <code>--help</code> always; <code>--version</code> for distributed tools; <code>set -x</code> behind a\n<code>DEBUG</code>/<code>TRACE</code> env guard, never unconditionally (secret leakage).</li>\n</ol>\n<h2>AUDIT mode</h2>\n<p>When reviewing existing shell scripts, hunt the defect classes in rules/ files bottom-up\n(each rules file ends with an audit checklist of grep patterns and ShellCheck codes).\nRun <code>shellcheck -S style</code> on every script if available; correlate findings with context —\nShellCheck flags symptoms, you judge exploitability and blast radius.</p>\n<p>Severity conventions:</p>\n<table>\n<thead>\n<tr>\n<th>Severity</th>\n<th>Meaning</th>\n<th>Examples</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>CRITICAL</td>\n<td>Exploitable or data-destroying now</td>\n<td><code>eval</code> on untrusted input; unquoted var in <code>rm -rf</code>; secrets in argv/<code>set -x</code>; curl|bash of unpinned URL in prod</td>\n</tr>\n<tr>\n<td>HIGH</td>\n<td>Will corrupt/fail on realistic input or failure</td>\n<td>unquoted expansions in destructive paths; missing <code>set -e</code>/error checks around critical steps; predictable temp files; non-atomic config writes; missing <code>exec</code> in entrypoint (signals lost)</td>\n</tr>\n<tr>\n<td>MEDIUM</td>\n<td>Latent bug or fragility</td>\n<td>parsing <code>ls</code>; <code>which</code> instead of <code>command -v</code>; missing <code>pipefail</code>; no <code>--</code> separators; no timeouts on network calls; <code>echo</code> for variable data</td>\n</tr>\n<tr>\n<td>LOW</td>\n<td>Style/maintainability with safety implications</td>\n<td>missing <code>local</code>; <code>[ ]</code> where <code>[[ ]]</code> intended; missing <code>readonly</code>; inconsistent error messages</td>\n</tr>\n</tbody>\n</table>\n<p>Finding format:</p>\n<pre><code>[SEVERITY] file:line — short title (SCxxxx if applicable)\n  Evidence: the offending line(s), verbatim\n  Impact: what input/condition triggers it and what breaks\n  Fix: concrete replacement code\n  Effort: trivial | small | medium | large\n</code></pre>\n<h2>Rules index</h2>\n<table>\n<thead>\n<tr>\n<th>File</th>\n<th>Covers</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><a href=\"rules/01-safety-baseline.md\">rules/01-safety-baseline.md</a></td>\n<td>Shebang discipline, <code>set -euo pipefail</code> and its real limitations, quoting &amp; word-splitting bug catalog, <strong>zsh-vs-bash deviations that bite pasted commands (joining, <code>pipestatus</code>, and <code>NOMATCH</code> — an unquoted glob in a flag value aborts the command and fakes a clean sweep)</strong></td>\n</tr>\n<tr>\n<td><a href=\"rules/06-ad-hoc-commands.md\">rules/06-ad-hoc-commands.md</a></td>\n<td>The commands nobody commits — a sweep, a probe, a one-liner from a checklist, a quick container copy: the zsh deviations that bite pasted commands (joining, <code>pipestatus</code>, <code>NOMATCH</code> faking a clean sweep), <strong>what your searcher silently excludes</strong> (<code>-r</code> vs symlinked dirs, ripgrep's gitignore defaults, a <code>grep</code> that is really a wrapper) and controlling a sweep in the same invocation, and <strong>blast radius</strong> — an ad-hoc command that fills a disk or corrupts the runtime it was inspecting</td>\n</tr>\n<tr>\n<td><a href=\"rules/05-constructs-and-cleanup.md\">rules/05-constructs-and-cleanup.md</a></td>\n<td>The constructs a script is assembled from, once the expansion itself is right: arrays for command building (SC2089/2090), IFS scoping, <code>trap</code>-based cleanup and <code>mktemp</code> (predictable temp paths are a race, not a style point), <code>[[ ]]</code>/printf/<code>local</code>/<code>readonly</code>, and globbing pitfalls including never parsing <code>ls</code></td>\n</tr>\n<tr>\n<td><a href=\"rules/02-robustness-correctness.md\">rules/02-robustness-correctness.md</a></td>\n<td>Argument parsing (getopts/while-case, --help/--version), input validation, POSIX vs bash portability, stderr/exit-code discipline, PIPESTATUS, command -v, network timeouts &amp; retries, flock &amp; background jobs, idempotency &amp; atomic writes, safe filename handling</td>\n</tr>\n<tr>\n<td><a href=\"rules/03-security.md\">rules/03-security.md</a></td>\n<td>eval/injection, secrets discipline (argv/env/set -x), PATH hygiene, sudo discipline, curl|bash both directions, temp-file races, umask, ShellCheck+shfmt in CI</td>\n</tr>\n<tr>\n<td><a href=\"rules/04-ci-and-operational.md\">rules/04-ci-and-operational.md</a></td>\n<td>GitHub Actions shell pitfalls (<code>${{ }}</code> injection, multiline run, quoting in YAML), container entrypoints (exec, PID 1, privilege drop), Makefile shell gotchas, long-running script logging</td>\n</tr>\n</tbody>\n</table>\n<h2>Top 10 non-negotiables</h2>\n<ol>\n<li><code>#!/usr/bin/env bash</code> + <code>set -euo pipefail</code> on every bash script — and explicit error\nhandling where <code>-e</code> is known not to fire (conditions, <code>&amp;&amp;</code>/<code>||</code>, command substitution\nin assignments-with-modifiers, process substitution).</li>\n<li>Quote <strong>every</strong> expansion: <code>\"$var\"</code>, <code>\"$@\"</code>, <code>\"${arr[@]}\"</code>, <code>\"$(cmd)\"</code>. SC2086 is a\nbug, not style.</li>\n<li>Build argument lists with arrays; pass them as <code>\"${args[@]}\"</code>. Never accumulate a\ncommand in a string and <code>eval</code>/word-split it.</li>\n<li><code>trap cleanup EXIT</code> with an idempotent cleanup function; temp paths only via <code>mktemp</code>.</li>\n<li>Never <code>eval</code>, <code>bash -c</code>, or <code>sh -c</code> with interpolated untrusted data; use <code>--</code> before\npositional file/user arguments to every command that supports it.</li>\n<li>No secrets in argv, in environment dumps, or under <code>set -x</code>; <code>set +x</code> around sensitive\nsections; read secrets from files or fds.</li>\n<li>Errors to stderr with context (<code>script: failed to X: $detail</code>); meaningful exit codes;\nnever <code>exit 0</code> on failure paths.</li>\n<li>Network calls get <code>--fail</code>, <code>--max-time</code>/timeouts, and bounded retries — never bare\n<code>curl url | ...</code>.</li>\n<li>Handle arbitrary filenames: <code>find -print0 | xargs -0</code> / <code>-exec ... +</code>, <code>while IFS= read -r</code>,\nnever iterate <code>$(ls)</code> or unquoted globs from variables.</li>\n<li>ShellCheck (clean, or annotated suppressions) + shfmt enforced in CI; a shell script\nwithout CI linting is unreviewed code.</li>\n</ol>\n<h2>Cross-references</h2>\n<ul>\n<li>CI pipeline hardening, <code>${{ }}</code> injection, action pinning → <code>sota-devsecops</code></li>\n<li>Secret storage/rotation → <code>sota-secrets-management</code></li>\n<li>Container image/runtime hardening → <code>sota-sandboxing</code>, <code>sota-cloud-infrastructure</code></li>\n<li>When the \"don't use shell\" rule fires → <code>sota-python</code> / <code>sota-golang</code></li>\n</ul>\n","files":[{"path":"rules/01-safety-baseline.md","sizeBytes":32300,"isText":true},{"path":"rules/02-robustness-correctness.md","sizeBytes":28887,"isText":true},{"path":"rules/03-security.md","sizeBytes":25780,"isText":true},{"path":"rules/04-ci-and-operational.md","sizeBytes":16477,"isText":true},{"path":"rules/05-constructs-and-cleanup.md","sizeBytes":24331,"isText":true},{"path":"rules/06-ad-hoc-commands.md","sizeBytes":46991,"isText":true},{"path":"rules/07-powershell.md","sizeBytes":16995,"isText":true},{"path":"rules/08-ad-hoc-side-effects.md","sizeBytes":14949,"isText":true},{"path":"rules/09-listing-and-selection.md","sizeBytes":20511,"isText":true},{"path":"SKILL.md","sizeBytes":15219,"isText":true}],"reviewScore":null,"reviewSummary":null,"trust":{"provenance":"trusted-source-unreviewed","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow.","bodySource":null},"bodyLocked":false,"purchaseUrl":null,"sourceUrl":null,"report":{"provenance":"trusted-source-unreviewed","screen":{"ran":true,"outcome":"clean","suspicious":0,"notes":0,"hiddenCharacters":false},"virusScan":{"engine":"clamav","status":"clean","scannedAt":"2026-11-11T21:45:13.164799Z","sha256":"CE46CC7D82A5D539C3A4BD39999CB9EFF4526815B73181F4F0CDF93D9A11A291","sizeBytes":106310},"review":null,"source":{"repositoryUrl":"https://github.com/martinholovsky/SOTA-skills","path":"skills/sota-shell-scripting","license":"CC-BY-4.0","commit":"78bebb3e5e04599b03bc5bb88a774e7b0f71b513","subtreeSha":"E8B7A3FF0A50E677198234DC828DB9A11DEAECE9549F035BCEBC62B52CC846F6","lastSyncedAt":"2026-11-11T21:44:03.380221Z"},"reviewedAt":"2026-11-11T21:46:54.266755Z","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow."},"install":[{"target":"skills-cli","command":"npx skills add https://github.com/martinholovsky/SOTA-skills/tree/main/skills/sota-shell-scripting"},{"target":"claude-code","command":"claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install martinholovsky-sota-skills@llmmart"},{"target":"git","command":"git clone https://github.com/martinholovsky/SOTA-skills.git"}]}