Home › Bash / shell scripts › Bash and shell scripts Windsurf rules

Bash and shell scripts Windsurf rules

Windsurf rules for Bash scripts: strict mode, quoting, shellcheck and shfmt, safe temp files and cleanup traps, argument parsing, portability, and bats tests.

WindsurfBash / shell scripts

How to use

Save this as .windsurf/rules/bash.md in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

Note: Review and test before use. Provided as is, without warranty. See Terms.

.windsurf/rules/bash.mdDownload  
---
trigger: glob
globs: **/*.sh,**/*.bash,bin/*,scripts/*
---

# Bash scripts

- Shebang `#!/usr/bin/env bash` and `set -Eeuo pipefail` at the top. If the script must run under POSIX `sh`, say so in a comment and avoid Bash-only syntax.
- Quote every expansion: `"$var"`, `"$@"`, `"${arr[@]}"`. Use `[[ ... ]]` for tests in Bash, `$(...)` not backticks, and `${var:-default}` for optional values.
- Every script passes `shellcheck` with no warnings (disable a check only with an inline comment explaining why) and is formatted with `shfmt -i 2 -ci` or the repo's settings.
- Put logic in functions with `local` variables and a `main "$@"` at the bottom. Constants in `readonly UPPER_CASE`; locals in `lower_case`.
- Temporary files and dirs via `mktemp`/`mktemp -d`, cleaned with `trap 'rm -rf "$tmp"' EXIT`. Never write to fixed paths in `/tmp`.
- Parse options with `getopts` (or a documented `while/case` loop) and print `usage()` on `-h` and on bad input. Exit codes: 0 success, 1 failure, 2 usage error.
- Errors to stderr: `echo "error: ..." >&2` (or a `die` helper). Use `printf` for anything with variables or escapes.
- Never parse `ls` output; iterate with globs (`for f in ./*.txt`) or `find ... -print0 | xargs -0` / `while IFS= read -r -d ''`. Use `read -r` always.
- Destructive commands (`rm -rf`, `git push --force`, `kubectl delete`) guard against empty variables (`"${dir:?}"`) and support a `--dry-run` flag where practical.
- Check required tools up front: `command -v jq >/dev/null || die "jq is required"`.
- Portability: note GNU vs BSD differences (`sed -i`, `date`, `readlink -f`) when scripts run on macOS and Linux; prefer portable forms.
- No secrets in scripts or `set -x` output; read them from the environment.
- Scripts longer than ~200 lines or needing data structures are a sign to switch to Python/Go; suggest it rather than growing Bash.

## Tests
`bats` tests in `test/` for scripts with logic, run with `bats test/`.

More: all Bash / shell scripts rules · all Windsurf rules

Related rules

Ansible Windsurf rules

Windsurf rules for Ansible playbooks and roles: FQCN modules, idempotency, check mode, vault secrets, and ansible-lint.

WindsurfAnsible

GitLab CI/CD pipelines Windsurf rules

Windsurf rules for .gitlab-ci.yml: rules over only/except, workflow rules, needs for DAG pipelines, lockfile-keyed caches, artifacts, protected variables, and includes.

WindsurfGitLab CI/CD

AWS CDK (TypeScript) CLAUDE.md

CLAUDE.md for AWS CDK v2 apps in TypeScript: constructs, least-privilege grants, snapshot tests, and safe deploys.

Claude Code (CLAUDE.md)AWS CDK

Azure Functions AGENTS.md

AGENTS.md for Azure Functions apps: current programming models, local Core Tools runs, idempotent triggers, managed identity, configuration, and Durable Functions rules.

AGENTS.mdAzure (Functions / Bicep)

Azure Bicep Copilot path instructions

Copilot instructions for Azure Bicep: modules and parameter files, secure parameters, existing resources, what-if before deploy, linter rules, and naming and tagging.

GitHub CopilotAzure (Functions / Bicep)

Docker & Compose AGENTS.md

AGENTS.md for containerized projects: lean multi-stage Dockerfiles, Docker Compose for local dev, and image security.

AGENTS.mdDocker

Get the best new AI prompts every week

One short email a week: the best new prompts and rules for ChatGPT, Claude, Gemini, Midjourney, Cursor and more, plus what changed in the tools. Free, unsubscribe anytime.

Finishes on our newsletter page at beehiiv (opens in a new tab). Or subscribe directly.