{"name":"write-safe-bash-scripts","description":"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.","category":"deployment","tags":["shell","ci","security"],"models":["any"],"proven":false,"passing_families":0,"model_testing":"Not yet model-tested; testing Jan 2027","url":"https://theskillvault.ai/skills/write-safe-bash-scripts","api_url":"https://theskillvault.ai/api/skills/write-safe-bash-scripts","raw_url":"https://theskillvault.ai/api/skills/write-safe-bash-scripts/raw","body":"# Write safe Bash scripts\n\nA 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.\n\n## When to use\n- You are writing a deploy, backup or cleanup script.\n- A script kept going after a command failed, and later steps used bad data.\n- Variables with spaces break a command because they were not quoted.\n\n## When not to use\n- You need a container image. That is a different task.\n- The script must run under plain POSIX `sh` on a tiny device. Say so, and skip Bash-only syntax.\n\n## Steps\n1. Start with `#!/usr/bin/env bash` and `set -Eeuo pipefail`. `-e` stops on a failed command, `-u` stops on an unset variable, `pipefail` fails the pipe if any part fails, and `-E` lets functions inherit the error trap.\n2. Quote every expansion: `\"$file\"`, never `$file`. Quote `\"$@\"` when you pass arguments on.\n3. Require inputs up front. `: \"${SRC:?Set SRC to the file to copy}\"` exits with a clear line when the variable is missing.\n4. Create temp files with `mktemp`. Register one `trap` on `EXIT` that deletes them. Do not use `/tmp/out` with a fixed name.\n5. Check that a needed program exists with `command -v`, not `which`.\n6. Prefer `[[ ]]` tests in Bash. Use `printf` instead of `echo` when the text must look the same on every machine.\n7. Add a `--dry-run` flag for anything that deletes or overwrites. Print the command, and skip the real one.\n8. Make the script safe to run twice. Creating a directory should use `mkdir -p`. Copying should not destroy a newer file by surprise.\n9. 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.\n\n```bash\n#!/usr/bin/env bash\nset -Eeuo pipefail\nsrc=\"${1:?Pass the file to archive}\"\ndest=\"${2:?Pass the destination directory}\"\ntmp=\"$(mktemp)\"\ntrap 'rm -f \"$tmp\"' EXIT\n[[ -f \"$src\" ]] || { printf 'No such file: %s\\n' \"$src\" >&2; exit 1; }\nmkdir -p -- \"$dest\"\ncp -- \"$src\" \"$tmp\"\nmv -- \"$tmp\" \"$dest/$(basename -- \"$src\").bak\"\ntrap - EXIT\n```\n\n## Gotchas\n- `set -e` does not fire inside some `if` tests. That is useful. Do not wrap real work in `if` just to hide a failure.\n- A pipe to `grep` can look successful when the earlier command failed, unless `pipefail` is on.\n- Never put a password, token or API key in the script. Read it from the environment at run time, and do not `printf` it.\n- `rm -rf \"$dir\"` is safe only when `dir` is checked and non-empty. An unset variable under `set -u` is a feature, not a nuisance. Do not turn `-u` off to \"make it work\".\n- macOS ships an old Bash when you call `bash` from `/bin`. `#!/usr/bin/env bash` uses the one on `PATH`. Install a current Bash if you need newer syntax.\n\n## Portability\nThese 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.\n\n## Done when\n- The script starts with strict mode, and a failed command stops it.\n- Variables are quoted, temp files are removed on exit, and a dry run does not change files.\n- No secret is stored in the file.\n\n## Example prompt\n\"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.\"\n\n---\nAdapted from \"bash-defensive-patterns\" in wshobson/agents (MIT, Copyright (c) 2024 Seth Hobson). See LICENSE.txt.","files":["LICENSE.txt"],"license":"MIT","license_name":"MIT License","license_url":"https://opensource.org/license/mit","attribution":"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","install":{"claude_code":"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":"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'","agents":"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'","mcp_url":"https://theskillvault.ai/mcp","cursor_mcp_deeplink":"cursor://anysphere.cursor-deeplink/mcp/install?name=theskillvault&config=eyJ1cmwiOiJodHRwczovL3RoZXNraWxsdmF1bHQuYWkvbWNwIn0%3D","mcp_config":{"mcpServers":{"theskillvault":{"url":"https://theskillvault.ai/mcp"}}}},"bundles":[],"tested_on":[{"family":"chatgpt","name":"ChatGPT","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"claude","name":"Claude","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"gemini","name":"Gemini","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"grok","name":"Grok","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"llama","name":"Llama","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"deepseek","name":"DeepSeek","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"mistral","name":"Mistral","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"qwen","name":"Qwen","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"kimi","name":"Kimi K","outcome":null,"date":null,"source":null,"counts":false,"stale":false}],"proven_rule":{"threshold":5,"window_days":365,"computed_at":"2026-11-29T03:42:30.374Z"}}