Coding Standards

Shell & Scripting Standards (Bash / PowerShell)

Intermediate

Scripts run our builds, deploys, automation, and one-off ops tasks. A careless script can delete data, leak a secret, or stop halfway. Treat scripts like real code: fail fast, quote everything, handle errors, keep secrets out, and make them safe to run again. This applies to Bash and PowerShell.

Scripts are written quickly and trusted widely. That is why they are risky. A script that does not stop on error can keep running after a failed step and leave a mess. An unquoted variable can turn into a destructive command. A secret printed to a log is a leak. These rules make scripts predictable and safe. Anything important a script does belongs in the pipeline, reviewed like other code (see CI/CD & Deployment).

Prefer well-tested tools over long custom scripts. When you do write a script, make it robust.

Fail fast & predictably

Safety with side effects

Secrets & security

Readability & maintenance

A safe script, end to end

No safety, unquoted, secret inline #!/bin/bash
API_KEY=sk_live_123 # hard-coded secret
rm -rf $BUILD_DIR/* # unquoted and maybe unset: disaster
deploy --key $API_KEY # key visible in process list and logs

There is no set -euo pipefail, so errors are ignored. It has a committed live secret. It runs rm -rf on an unquoted, possibly empty path, which can delete the wrong thing. And the key is shown on the command line. Several serious problems.

Safe, quoted, secret from env #!/bin/bash
set -euo pipefail
: "${BUILD_DIR:?BUILD_DIR is required}" # fail if unset
API_KEY="$(get-secret deploy-key)" # from vault, not echoed
rm -rf "${BUILD_DIR:?}"/*
deploy --key-env API_KEY # passed via env, not argv

This fails fast, refuses to run with an unset path, reads the secret from the vault without printing it, and quotes everything. It is safe to run and safe to run again.

Self-review checklist

Why it matters: Scripts have a lot of power and little protection, so their failures are often destructive: deleted directories, leaked keys, half-done deploys. Treat scripts as real, reviewed code that fails fast, quotes everything, guards destructive actions, and keeps secrets out. This turns a common source of incidents into reliable automation.