Skip to content

skillDevOps and cloudbash

Shell Script Hardening

Hardens bash scripts with strict mode, safe quoting, cleanup traps and clean shellcheck results, without changing what the script does.

npx workspacetemplates add shell-script-hardening --tool claude-code

Writes 1 file to .claude/skills/shell-script-hardening/

.claude/skills/shell-script-hardening/SKILL.md29 lines · 1.7 KBFile
---
name: shell-script-hardening
description: Hardens bash scripts with strict mode, safe quoting, cleanup traps and clean shellcheck results, without changing what the script does.
---

# Shell script hardening

Keep behavior identical. Remove ways for the script to fail silently or do damage.

## Steps

1. Read the script fully and note what it does. If there are no tests, write a small harness or a dry-run first.
2. Check the shebang: `#!/usr/bin/env bash`. If it uses bash features, do not claim POSIX sh.
3. Add strict mode after the shebang: `set -Eeuo pipefail`, and a safe `IFS=$'\n\t'` only if the script relies on word splitting you have verified.
4. Quote every expansion: `"$var"`, `"${arr[@]}"`. Use arrays for command arguments, never strings. Prefer `$(...)` to backticks and `[[ ]]` to `[ ]`.
5. Validate input: required arguments with `${1:?usage: script <dir>}`, existence of files and tools (`command -v jq >/dev/null`), and refuse empty variables in `rm -rf "${dir:?}"/`.
6. Create temp files with `mktemp` and register `trap 'rm -rf "$tmp"' EXIT`. Add `trap ... ERR` to print the failing line when useful.
7. Handle expected failures explicitly with `if cmd; then` or `cmd || true` and a comment. Do not hide errors with `2>/dev/null`.
8. Run `shellcheck -x script.sh` until clean. Fix the cause; use a disable comment only with a reason.
9. Test with file names that contain spaces, quotes and a leading dash, an unset variable and a failing middle command in a pipe.

## Rules

- Never add features while hardening.
- Use `--` before file arguments to commands that accept options.

## Report

Changes grouped by risk fixed, the shellcheck result, and the tests run.

What it does

Shell scripts fail in quiet ways: an unquoted variable splits on a space, a failed command in a pipe is ignored, a temp file is left behind. This skill goes through a script the way shellcheck and a careful reviewer would, and fixes the causes while keeping its behavior.

It adds strict mode, argument checks, a cleanup trap and a usage message, then runs shellcheck until it is clean and tests the script with awkward input such as spaces in file names.

Example

you › Harden scripts/backup.sh before we run it from cron

agent › Added set -Eeuo pipefail, quoted 23 expansions, replaced the fixed /tmp/backup.tmp with mktemp and an EXIT trap, guarded rm -rf with ${dest:?}. shellcheck: 11 warnings to 0. Tested with a directory named "my files" and a failing tar step; the script now stops with a clear error.

Questions

Can set -e break my script?
It can expose failures that were previously ignored. That is the point, but the agent checks each place where a nonzero exit is expected and handles it on purpose.
Is shellcheck required?
It is used when installed. Without it, the agent applies the same checks by hand and says shellcheck was not run.
  • CI Pipeline Speedup

    SkillDevOps and cloud

    Free
  • Deployment Strategies

    SkillDevOps and cloud

    Free