434-bash-safety

Rules for writing safer Bash scripts, which are command-line programs commonly used for automation. They require strict error handling, quoted variables, dependency checks, and reusable functions.

In plain words
What is it for?
Use them when creating or changing Bash scripts that automate builds, deployments, file operations, or other development tasks.
Why use it?
They help scripts stop on failures and avoid common problems caused by missing values, unsafe text expansion, or unavailable commands.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/hamzaamjad/cursor-rules/434-bash-safety
Clone the repo
git clone --depth 1 https://github.com/hamzaamjad/cursor-rules
Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 974 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.00974
Opus 5 $0.00000 $0.00487
Sonnet 5 $0.00000 $0.00195
Haiku 4.5 $0.00000 $0.00097

Measured yesterday against content hash 78beb5f55427, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

434-bash-safety scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

rules/400-patterns/434-bash-safety.mdc · 70 lines

What it actually says

  • Purpose: To promote the creation of safe, robust, and maintainable Bash scripts by enforcing strict modes, safe variable handling, and dependency checks.
  • Requirements:
    • Start all scripts with set -euo pipefail.
      • set -e: Exit immediately if a command exits with a non-zero status.
      • set -u: Treat unset variables as an error when substituting.
      • set -o pipefail: Return value of a pipeline is the exit status of the last command to exit with a non-zero status, or zero if no command exited with a non-zero status.
    • Always quote variable expansions (e.g., "$VAR", "${ARRAY[@]}") unless deliberate word splitting or globbing is intended and understood.
    • Prefer functions for reusable blocks of code over loose commands.
    • Explicitly check for the existence of required external commands before use (e.g., using command -v).
    • Use environment variables or dedicated config files for configuration/secrets; avoid hardcoding values.
    • Strive for idempotency where the script's purpose allows (can be run multiple times with the same initial state, yielding the same final state).
    • Debugging/Modification: When refactoring or adding complex conditional logic, test changes incrementally. If a script fails with a syntax error, carefully review the diff of the last change, paying close attention to quoting, parentheses, and block structures (if/then/else/fi, do/done).
  • Validation:
    • Check (Static Analysis): Use shellcheck (available at shellcheck.net) to automatically lint scripts. Configure CI to run shellcheck on all .sh files. Target SC2086 (unquoted variables), SC2154 (unset variables referenced with set -u), etc.
    • Check (Code Review): Verify set -euo pipefail is present. Review variable expansions for correct quoting. Ensure dependencies are checked. Look for hardcoded secrets/paths. Assess idempotency logic.
  • Examples:
    • Dependency Check:
      #!/bin/bash
      set -euo pipefail
      
      REQUIRED_CMD="jq"
      if ! command -v "$REQUIRED_CMD" &> /dev/null; then
          echo "Error: Required command '$REQUIRED_CMD' not found." >&2
          exit 1
      fi
      
      echo "Processing data..."
      # Safely use jq after check
      cat data.json | "$REQUIRED_CMD" '.items[] | .name'
      
    • Quoting:
      # Bad: Fails if filename contains spaces
      # FILE_PATH="my report.txt"
      # ls $FILE_PATH
      
      # Good: Handles spaces correctly
      FILE_PATH="my report.txt"
      ls "$FILE_PATH"
      
      # Good: Iterating over array elements safely
      FILES=("file one.txt" "file two.txt")
      for f in "${FILES[@]}"; do
          echo "Processing '$f'"
          # Perform action on "$f"
      done
      
  • Changes: Explained set -euo pipefail, specified validation via shellcheck and CI integration, provided more detailed examples for dependency checks and variable quoting (including array iteration).
  • Source References: .cursor/rules/bash-safety.mdc; ShellCheck Website; ShellCheck GitHub; Bash scripting quirks & safety tips (jvns.ca)

Purpose: Make shell scripts robust, debuggable, and secure.

  • Begin scripts with set -euo pipefail; use IFS safely.
  • Quote all variable expansions: "$var".
  • Avoid sudo inside scripts; require explicit invocation.
  • Trap signals (trap 'cleanup' EXIT) for resource cleanup.
  • Validate required commands/files exist; exit with error if not.
  • Do not echo secrets; read sensitive data from secure stores or env vars.
- Use `set -euo pipefail`
Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 70 lines · 0 tokens per session scan A 78beb5f55427

Subscribe to this mod's changes

434-bash-safety is a cursor rule published in the GitHub repository hamzaamjad/cursor-rules (2 stars, last pushed 1y ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 974 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.