- Home
- Skills
- Code Quality & Review
- Bash Script Hardener with ShellCheck and Bats Tests
Works with the AI Agents you already use
Bash Script Hardener with ShellCheck and Bats Tests
Hardens or writes bash and sh scripts with ShellCheck, quoting, safe rm, traps, getopts, dry-run and macOS fixes, plus tests. Verdict SAFE / FIX FIRST / DANGEROUS
$5
Bash Script Hardener with ShellCheck and Bats Tests
Example session with this skill installed
Harden this cron script before I deploy it. It prunes old backups and runs on an Ubuntu server and on my MacBook:
#!/bin/bash
BACKUP_DIR=$1
DAYS=7
cd $BACKUP_DIR
for f in $(ls *.tar.gz); do
if [ $(find $f -mtime +$DAYS) ]; then rm -rf $f; fi
done
rm -rf $BACKUP_DIR/tmp/*
sed -i "s/last_run=.*/last_run=$(date -d now +%F)/" state.conf
- Read your context and instructions
- Compiled the bash script hardener
Verdict
Original: DANGEROUS: with no argument, cd $BACKUP_DIR goes to $HOME and line 8 runs rm -rf /tmp/*
Corrected: SAFE: ShellCheck 0.11.0 clean, 11/11 tests pass (bash 5.2, Linux). macOS not run here.
Findings
| # | Line | Severity | Issue | Code | Fix |
|---|---|---|---|---|---|
| 1 | 8 | DANGEROUS | empty BACKUP_DIR becomes rm -rf /tmp/* | manual | require arg, resolve path, "${BACKUP_DIR:?}/tmp" |
| 2 | 4 | HIGH | unchecked, unquoted cd | SC2164, SC2086 | no cd; absolute path, refuse / |
| 3 | 5 | HIGH | loop over ls output | SC2045, SC2035 | find -print0 + read -r -d '' |
| 4 | 6 | MED | unquoted $(find ...) in [ ] | SC2046, SC2086 | find -mtime +"$DAYS" does the test |
| 5 | 9 | MED | sed -i, date -d are GNU-only | manual | sed > "$tmp/x" && mv; date +%Y-%m-%d |
| 6 | all | LOW | no usage, dry-run, logging, exit codes | manual | getopts, -n/--dry-run, log() to stderr |
Corrected script (excerpt; full file delivered)
#!/usr/bin/env bash
set -Eeuo pipefail
...
(( $# == 1 )) || { usage >&2; exit 2; }
BACKUP_DIR=$(cd -P -- "$1" && pwd -P)
[[ $BACKUP_DIR != / ]] || die "refusing to prune /"
tmp=$(mktemp -d "${TMPDIR:-/tmp}/${SCRIPT_NAME}.XXXXXX")
trap cleanup EXIT
while IFS= read -r -d '' f; do
run rm -f -- "$f"; count=$((count + 1))
done < <(find "$BACKUP_DIR" -maxdepth 1 -type f \
-name '*.tar.gz' -mtime +"$DAYS" -print0)
Diff summary
- Safety: path is required, resolved and checked; no
rm -rfon a bare variable - Portability: no
sed -i/date -d; works with macOS bash 3.2 - Behavior changes: new
-d DAYS,-n/--dry-run,-h; exit 2 on bad usage
Tests
test/run-tests.sh (bats not installed): 11 passed, 0 failed, including a dry run that leaves files in place and a backup name with a space.
Connects securely to your tools. The creator never sees your data.
About this skill
Most shell scripts work right up until a variable is empty, a filename has a space, or the script runs on a Mac. Then rm -rf hits the wrong directory, a cron job fails silently, or sed -i errors on BSD. Asking an AI to "review my script" usually gets a list of tips, not a fixed file you can trust.
This skill gives your coding agent a fixed procedure and a reference file of checked facts. It:
- Checks for dangerous patterns first: unguarded rm -rf on variable paths, curl piped to bash, eval or sh -c on input, secrets in argv or set -x output
- Runs ShellCheck and shfmt if installed, or works through a manual checklist and says so
- Fixes strict mode (and notes where set -e will not fire), quoting, [[ ]] vs [ ], mktemp with EXIT/INT/TERM traps, ${VAR:?} guards, getopts with usage, --dry-run, stderr logging, exit codes, idempotency and safe file reading
- Flags GNU vs BSD differences (sed -i, date -d, readlink -f, stat) and bash 4+ features that break on macOS /bin/bash 3.2
- Returns the full corrected script, a findings table with line numbers, a diff summary, and a bats test file (or a plain bash test script if bats is not installed)
- Gives a verdict on both the original and the corrected file: SAFE, FIX FIRST or DANGEROUS
For developers, DevOps and SRE engineers, and anyone who writes cron jobs, CI steps, install scripts or container entrypoints.
The ShellCheck codes, tool versions and macOS facts in the reference file were checked against the ShellCheck wiki, release pages and bats-core docs in October 2026. The skill only cites a code if ShellCheck reported it or it is listed in the reference. It reports test results only when it actually ran them.
Covers bash and POSIX sh only. It flags macOS and BusyBox differences but can only test on the system your agent runs on. ShellCheck and bats results depend on those tools being installed; without them, checks are manual and labeled as such. It does not replace a security audit of what the script's commands do.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before any payment.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Money-back guarantee
Doesn't do what its page promises? You get your money back.
Trust & safety
Security scan
Security scan passed 1 month ago
Requires