/iac-automation-skills:shell-scripting-best-practicesfrom iac-automation-skills
Writes robust, portable Bash/POSIX shell scripts with strict-mode error handling, correct quoting, trap-based cleanup, and linting/testing via shellcheck and bats. Use for ops automation, CI steps, or hardening existing scripts.
Ships in iac-automation-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Shell scripts are the connective tissue of ops automation — glue between CLI tools, CI steps, and cron jobs — but Bash's defaults are unsafe for that role: a failed command doesn't stop the script, an unset variable silently expands to empty, and a failure in the middle of a pipeline is invisible unless explicitly checked. Getting the strict-mode/error-handling foundation right is what separates a script that fails loudly and safely from one that silently corrupts state, deletes the wrong path, or half-completes a multi-step operation and leaves the system in an unknown state.
sh (e.g. for
Alpine-based containers using dash, or minimal init systems) versus
relying on Bash-only features.shellcheck, bats) to a script before it's
trusted in a production pipeline.rm, mv, bulk cloud
CLI calls) before it runs unattended.set -o pipefail plus associative arrays and
${var@Q}-style quoting expansions if used; Bash 3.2 (macOS's shipped
default for a long time) lacks associative arrays entirely — confirm
the target shell/version before relying on Bash 4+ features, or target
POSIX sh explicitly if the script must run on minimal images.shellcheck (static analysis) and shfmt (formatting) installed
locally and in CI.bats-core for behavioral tests when a script has any nontrivial logic
(argument parsing, conditional branches, retry logic) worth asserting
against.#!/bin/sh resolves
to (dash on Debian/Ubuntu, bash on many other distros, busybox sh
in minimal containers) — behavior differs on [[ ]], arrays, and
local, none of which are POSIX.Start every Bash script with strict mode and a clear shebang:
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -e: exit immediately if any command fails (returns non-zero),
instead of continuing past it.set -u: treat an unset variable as an error instead of silently
expanding to an empty string — catches typos in variable names.set -o pipefail: a pipeline (cmd1 | cmd2) fails if any stage
fails, not just the last one — without this, false | true reports
success.IFS=$'\n\t': narrows word-splitting to newlines/tabs instead of
also splitting on spaces, reducing surprises when iterating over
filenames.Quote every variable expansion and command substitution unless word-splitting is explicitly wanted:
# Wrong — breaks on filenames with spaces, and is subject to globbing
rm -rf $target_dir/*
# Right
rm -rf -- "${target_dir:?}"/*
"${target_dir:?}" fails loudly with an error if target_dir is
unset or empty, instead of silently expanding rm -rf /* against the
filesystem root — critical for any variable feeding a destructive
command.
Warning: An unquoted variable in
rm,mv, or similar is one of the most common causes of catastrophic ops incidents. Always quote, always use:?on any variable that gates a destructive path, and prefer--before the path argument so a value starting with-can't be parsed as a flag.
Use traps for guaranteed cleanup, so temp files/locks are removed even if the script exits early or is interrupted:
tmp_dir="$(mktemp -d)"
cleanup() {
rm -rf -- "${tmp_dir}"
}
trap cleanup EXIT
trap 'echo "Interrupted" >&2; exit 130' INT TERM
The EXIT trap fires on normal exit, an error under set -e, or
exit called explicitly — one cleanup path instead of duplicating
cleanup logic before every possible exit point.
Check preconditions explicitly instead of assuming they hold:
require_cmd() {
command -v "$1" >/dev/null 2>&1 || {
echo "Error: required command '$1' not found in PATH" >&2
exit 1
}
}
require_cmd aws
require_cmd jq
Handle errors with context, not just set -e's bare exit. For
commands where failure needs a specific message or retry, check
explicitly rather than relying only on strict mode:
if ! aws s3 cp "${local_file}" "s3://${bucket}/${key}"; then
echo "Error: failed to upload ${local_file} to s3://${bucket}/${key}" >&2
exit 1
fi
For transient failures (network calls, cloud API rate limits), wrap with a bounded retry instead of failing on the first blip:
retry() {
local -r max_attempts="$1"; shift
local attempt=1
until "$@"; do
if (( attempt >= max_attempts )); then
echo "Error: '$*' failed after ${max_attempts} attempts" >&2
return 1
fi
echo "Attempt ${attempt} failed, retrying in $((attempt * 2))s..." >&2
sleep "$((attempt * 2))"
(( attempt++ ))
done
}
retry 3 aws s3 cp "${local_file}" "s3://${bucket}/${key}"
Decide Bash vs. POSIX sh deliberately, not by accident. If the
script needs arrays, [[ ]], local -r, process substitution, or
set -o pipefail, it is a Bash script — use #!/usr/bin/env bash and
don't claim POSIX compatibility. If it must run under a minimal sh
(e.g. dash in an Alpine container's entrypoint), write to the POSIX
subset deliberately:
#!/bin/sh
set -eu
# No arrays, no [[ ]] — use [ ] and case statements instead.
if [ -z "${TARGET_DIR:-}" ]; then
echo "Error: TARGET_DIR must be set" >&2
exit 1
fi
case "$1" in
start|stop|restart) action="$1" ;;
*) echo "Usage: $0 {start|stop|restart}" >&2; exit 2 ;;
esac
Note POSIX sh has no pipefail — check pipeline stages individually
via PIPESTATUS-free patterns (e.g. write intermediate output to a
temp file and check each command's exit status separately) if a
pipeline's middle-stage failure must be caught.
Lint and test before trusting a script in a pipeline:
shellcheck deploy.sh
shfmt -d deploy.sh
bats tests/deploy.bats
A minimal bats test asserting both correct behavior and safe
failure:
# tests/deploy.bats
@test "fails when TARGET_DIR is unset" {
run env -u TARGET_DIR ./deploy.sh
[ "$status" -ne 0 ]
[[ "$output" == *"TARGET_DIR must be set"* ]]
}
@test "uploads the built artifact" {
run env TARGET_DIR=/tmp/build ./deploy.sh
[ "$status" -eq 0 ]
}
$(...) over backticks for command substitution — nestable and
more readable, with identical semantics.local for every function-scoped variable in Bash so functions
don't leak state into the caller's scope.printf over echo when output must be exact/portable — echo's
handling of backslash escapes and flags differs across shells.--dry-run mode for anything destructive or
state-changing, printing what would happen without executing it —
mirrors the review-before-apply pattern used for
terraform plan/CloudFormation change sets/ansible-playbook --check.exit 2 for usage errors, exit 1
for runtime failures) instead of always exit 1, so calling automation
(CI, cron alerting) can distinguish failure classes.aws --version, jq --version in a preflight
check) rather than assuming whatever's on PATH behaves identically
everywhere the script runs.Symptom: A script deletes far more than intended after a variable
ended up empty.
Fix: This is the classic unquoted/unguarded rm -rf $var/* failure
mode — set -u alone doesn't catch a variable that's set but empty.
Use "${var:?variable must be set}" on any variable feeding a
destructive command, quote every expansion, and add a --dry-run mode
used for the first run against anything unfamiliar.
Symptom: A script "succeeds" (exit code 0) but a step in the middle
of a pipeline clearly failed based on the output.
Fix: Missing set -o pipefail — without it, only the last
command's exit status counts. Add set -o pipefail, and note it's
Bash-only (not POSIX sh); for POSIX scripts, break the pipeline into
separate steps with intermediate files/status checks.
Symptom: A script that ran fine locally fails on a colleague's
machine or in a minimal container with "command not found" for [[ or
arrays.
Fix: The script is Bash-flavored but is being invoked via sh (a
symlink to dash/busybox on many systems), or Bash-specific syntax
leaked into a script declared #!/bin/sh. Make the shebang match the
actual language used (#!/usr/bin/env bash for Bash features), and
don't mix POSIX and Bash-only syntax in the same file.
Symptom: A script silently continues after a command fails, even
though set -e is present.
Fix: set -e doesn't trigger inside if/while conditions, the
left side of &&/||, or a pipeline's non-last stage without
pipefail — these are well-known set -e blind spots. Check
command results explicitly in those contexts rather than relying on
set -e to catch everything.
Symptom: Temp files pile up in /tmp after a script errors out
partway through.
Fix: Cleanup logic was only placed at the end of the script's
"happy path," never reached on early exit. Move cleanup into an EXIT
trap (step 3) so it always runs regardless of how/where the script
exits.
Scenario: A deployment helper that packages a build directory, dry-run capable, uploads to S3 with retry, and cleans up its temp workspace no matter how it exits.
deploy.sh:
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
usage() {
echo "Usage: $0 [--dry-run] <build_dir> <s3_bucket>" >&2
exit 2
}
dry_run=false
if [[ "${1:-}" == "--dry-run" ]]; then
dry_run=true
shift
fi
build_dir="${1:?build_dir is required}"
bucket="${2:?s3_bucket is required}"
require_cmd() {
command -v "$1" >/dev/null 2>&1 || {
echo "Error: required command '$1' not found in PATH" >&2
exit 1
}
}
require_cmd aws
require_cmd tar
[[ -d "${build_dir}" ]] || { echo "Error: ${build_dir} is not a directory" >&2; exit 1; }
tmp_dir="$(mktemp -d)"
cleanup() { rm -rf -- "${tmp_dir}"; }
trap cleanup EXIT
trap 'echo "Interrupted" >&2; exit 130' INT TERM
archive="${tmp_dir}/build-$(date +%Y%m%d%H%M%S).tar.gz"
tar -czf "${archive}" -C "${build_dir}" .
retry() {
local -r max_attempts="$1"; shift
local attempt=1
until "$@"; do
if (( attempt >= max_attempts )); then
echo "Error: '$*' failed after ${max_attempts} attempts" >&2
return 1
fi
echo "Attempt ${attempt} failed, retrying in $((attempt * 2))s..." >&2
sleep "$((attempt * 2))"
(( attempt++ ))
done
}
if "${dry_run}"; then
echo "[dry-run] would upload ${archive} to s3://${bucket}/builds/"
else
retry 3 aws s3 cp "${archive}" "s3://${bucket}/builds/"
echo "Uploaded $(basename "${archive}") to s3://${bucket}/builds/"
fi
Lint and test:
shellcheck deploy.sh
bats tests/deploy.bats
tests/deploy.bats
@test "fails with usage error on missing args" {
run ./deploy.sh
[ "$status" -eq 2 ]
}
@test "dry-run does not call aws" {
run ./deploy.sh --dry-run ./fixtures/build example-bucket
[ "$status" -eq 0 ]
[[ "$output" == *"[dry-run] would upload"* ]]
}
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
14 skills in this plugin
/ansible-playbook-and-role-designDesigns and safely runs Ansible playbooks, roles, and inventories for configuration management, including idempotent task design, Ansible Vault for secrets, and choosing config-management (Ansible) versus provisioning (Terraform/CloudFormation) for a given change. Use when the user asks to "write an Ansible playbook/role for X," "structure an Ansible inventory across environments," "make this task idempotent," "encrypt secrets with Ansible Vault," "dry-run a playbook before production," or "should this be Ansible or Terraform."
/aws-cloudformation-templatesAuthors and safely operates AWS CloudFormation templates (YAML/JSON), including stacks, change sets, nested and cross-stack references, StackSets for multi-account/multi-region deployment, and drift detection. Use when the user asks to "write a CloudFormation template for X," "structure nested CloudFormation stacks," "deploy the same stack across AWS accounts/regions," "review a CloudFormation change set before applying," "detect/reconcile drift on a stack," or "migrate/compare CloudFormation with Terraform" for an AWS-native IaC choice.
/chef-puppet-saltstack-legacy-config-managementProvides a comparative overview of Chef, Puppet, and SaltStack for teams maintaining existing legacy configuration-management estates alongside or instead of Ansible — agent-based pull architecture, DSL/resource models (Chef recipes/cookbooks, Puppet manifests/catalogs, Salt states/pillars), and a candid assessment of where each still earns its keep in a modern enterprise versus where it's inertia. Use when the user asks to "maintain this existing Chef/Puppet/Salt estate," "write a Chef recipe/Puppet manifest/Salt state," "should we migrate off Puppet to Ansible," "why is our Puppet run applying a change we didn't expect," or "compare Chef vs. Puppet vs. SaltStack."
/feature-flag-configuration-launchdarkly-and-unleashDesigns and operates feature flag configuration with LaunchDarkly or Unleash, covering flag lifecycle (create, target, roll out, retire), kill-switch patterns for fast incident mitigation without a redeploy, targeting/segmentation rules, and cleaning up stale flag debt before it accumulates into unreadable conditional logic. Use when the user asks to "add a feature flag for X," "set up a kill switch for this feature," "configure LaunchDarkly/Unleash targeting rules," "roll out a flag gradually by percentage," "clean up old feature flags," or "should we use LaunchDarkly or self-host Unleash."
/gremlin-chaos-engineering-configurationConfigures Gremlin-specific chaos engineering attacks — installing the Gremlin agent/sidecar, defining resource, state, and network attacks via the Gremlin API/CLI or Terraform provider, scoping blast radius with targets and halt conditions, and running Gremlin Scenarios for multi-step failure sequences. Use when the user asks to "configure a Gremlin attack," "set up the Gremlin agent in Kubernetes/EC2," "write a Gremlin Scenario," "scope a Gremlin blast radius/halt condition," or "use Gremlin to test our failover." For the general chaos-engineering principles (steady-state hypothesis, game days, tool-agnostic blast radius scoping) this configures against, use the SRE chaos-engineering-and-resilience-testing skill instead.
/infrastructure-post-deployment-validation-and-smoke-testingVerifies that a Terraform apply, CloudFormation deploy, or Ansible playbook run actually produced working infrastructure — resource existence and configuration checks, connectivity/health smoke tests, and automated post-apply assertions — rather than treating a clean "apply succeeded" exit code as proof the system works. Use when the user asks to "verify this deployment actually worked," "write a smoke test after terraform apply/cloudformation deploy," "check the resource is actually reachable/configured correctly post-deploy," "add post-deployment validation to the pipeline," or "our apply succeeded but the service is down."
npx claudepluginhub selvarajmurugesan90/ops-engineering-skills --plugin iac-automation-skillsOther plugins' skills for the same job