Skip to content
Back to skills

Shell Script Hardening

BSecurity

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

  • 3 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added November 15, 2026
ai-agentsshellbash

Security analysis

B85/100
  • highPerforms destructive filesystem operations

Pro shows the line behind each finding and how to fix it

Scanned November 15, 2026

npx -y skills add workspace-templates/ai-agent-templates --skill shell-script-hardening --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Shell Script Hardening?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Shell Script Hardening
[![Security: B — Skills Directory](https://www.skillsdirectory.com/api/skills/workspace-templates-shell-script-hardening/badge)](https://www.skillsdirectory.com/skills/workspace-templates-shell-script-hardening)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
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.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.