Deployment and dev setup · Skill
Write safe Bash scripts
write-safe-bash-scripts
Writes Bash scripts that stop at the first real failure, with strict mode, quoted variables, safe temp files and a cleanup trap. Use when the user wants a safe Bash script, set -euo pipefail, or a shell script that must not continue after an error.
Works with: Any AI Not yet model-tested; testing Jan 2027
Paste it into ChatGPT, Claude, Gemini or any AI chat, then tell it about your project. How it works
Install in Claude Code, Cursor or Codex (optional)
Only for AI coding tools that read skill folders. Run one line in a terminal, in your project folder. It downloads this skill's text files from TheSkillVault and nothing else; you can read them first with “View raw SKILL.md”.
Claude Code (all your projects)
curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/write-safe-bash-scripts/raw' -o ~/'.claude/skills/write-safe-bash-scripts/SKILL.md' && curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/write-safe-bash-scripts/files/LICENSE.txt' -o ~/'.claude/skills/write-safe-bash-scripts/LICENSE.txt'Cursor (this project)
curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/write-safe-bash-scripts/raw' -o '.cursor/skills/write-safe-bash-scripts/SKILL.md' && curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/write-safe-bash-scripts/files/LICENSE.txt' -o '.cursor/skills/write-safe-bash-scripts/LICENSE.txt'Codex and other Agent Skills tools (this project)
curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/write-safe-bash-scripts/raw' -o '.agents/skills/write-safe-bash-scripts/SKILL.md' && curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/write-safe-bash-scripts/files/LICENSE.txt' -o '.agents/skills/write-safe-bash-scripts/LICENSE.txt'
Or connect the whole library over MCP
Add TheSkillVault to Cursor or add this to your app's MCP settings (read-only; help):
{
"mcpServers": {
"theskillvault": {
"url": "https://theskillvault.ai/mcp"
}
}
}A Bash script should stop when something fails, not keep going and wreck the next step. A few lines at the top prevent most of the silent damage.
When to use
- You are writing a deploy, backup or cleanup script.
- A script kept going after a command failed, and later steps used bad data.
- Variables with spaces break a command because they were not quoted.
When not to use
- You need a container image. That is a different task.
- The script must run under plain POSIX
shon a tiny device. Say so, and skip Bash-only syntax.
Steps
- Start with
#!/usr/bin/env bashandset -Eeuo pipefail.-estops on a failed command,-ustops on an unset variable,pipefailfails the pipe if any part fails, and-Elets functions inherit the error trap. - Quote every expansion:
"$file", never$file. Quote"$@"when you pass arguments on. - Require inputs up front.
: "${SRC:?Set SRC to the file to copy}"exits with a clear line when the variable is missing. - Create temp files with
mktemp. Register onetraponEXITthat deletes them. Do not use/tmp/outwith a fixed name. - Check that a needed program exists with
command -v, notwhich. - Prefer
[[ ]]tests in Bash. Useprintfinstead ofechowhen the text must look the same on every machine. - Add a
--dry-runflag for anything that deletes or overwrites. Print the command, and skip the real one. - Make the script safe to run twice. Creating a directory should use
mkdir -p. Copying should not destroy a newer file by surprise. - Run it once with a missing variable, once with a failing command, and once on the happy path. All three should behave as you expect.
#!/usr/bin/env bash
set -Eeuo pipefail
src="${1:?Pass the file to archive}"
dest="${2:?Pass the destination directory}"
tmp="$(mktemp)"
trap 'rm -f "$tmp"' EXIT
[[ -f "$src" ]] || { printf 'No such file: %s\n' "$src" >&2; exit 1; }
mkdir -p -- "$dest"
cp -- "$src" "$tmp"
mv -- "$tmp" "$dest/$(basename -- "$src").bak"
trap - EXIT
Gotchas
set -edoes not fire inside someiftests. That is useful. Do not wrap real work inifjust to hide a failure.- A pipe to
grepcan look successful when the earlier command failed, unlesspipefailis on. - Never put a password, token or API key in the script. Read it from the environment at run time, and do not
printfit. rm -rf "$dir"is safe only whendiris checked and non-empty. An unset variable underset -uis a feature, not a nuisance. Do not turn-uoff to "make it work".- macOS ships an old Bash when you call
bashfrom/bin.#!/usr/bin/env bashuses the one onPATH. Install a current Bash if you need newer syntax.
Portability
These flags are Bash. If you use POSIX sh, drop [[ ]], arrays and mapfile. Keep quoting, set -eu, mktemp, and tests for missing inputs. The same habits apply in zsh scripts, with that shell's options.
Done when
- The script starts with strict mode, and a failed command stops it.
- Variables are quoted, temp files are removed on exit, and a dry run does not change files.
- No secret is stored in the file.
Example prompt
"Write a Bash script that archives a directory to a dated folder. It must stop if the copy fails, quote every path, and support --dry-run."
---
Adapted from "bash-defensive-patterns" in wshobson/agents (MIT, Copyright (c) 2024 Seth Hobson). See LICENSE.txt.
Bundled files
Licence
Adapted from Seth Hobson (wshobson/agents, bash-defensive-patterns) (Copyright (c) 2024 Seth Hobson), MIT License. Adapted by TheSkillVault: rewritten as a short checklist with one original script; dropped the pattern catalogue and reference file; added secret handling, a dry-run note, gotchas and done-when.
Licence: MIT License. Attribution when you reuse it: Based on work by Seth Hobson (wshobson/agents, bash-defensive-patterns) (Copyright (c) 2024 Seth Hobson, MIT License), https://github.com/wshobson/agents/tree/7e2fb559cf4b824b27f1fec49fe88f7511066d26/plugins/shell-scripting/skills/bash-defensive-patterns; adapted by TheSkillVault, https://theskillvault.ai