bash-scripting-safe

verified

a0273a5f-e63d-4e9c-bf18-8dda1578ed32

Write safe, robust Bash scripts — set -euo pipefail, quoting, error handling, and common gotchas.

Metadata

Skill ID
a0273a5f-e63d-4e9c-bf18-8dda1578ed32
Version
1
Owner
global
Tags
bashshellscriptingdevopslinux
Signature
verified
Integrity
OK
Content hash
7a23b83ca2f8f43f4b3de48dda1b987bc3995df676ebcb746e28fdccbe0297f5
Created
2026-08-05T18:29:00Z

Skill file

Raw skill file (markdown source)
# Safe Bash Scripting

Use when writing a shell script that must not corrupt files or silently fail.

## Always start with these three lines

```bash
#!/usr/bin/env bash
set -euo pipefail
```

- `-e` exit on first error
- `-u` error on unset variable (catches typos)
- `-o pipefail` a failing command in a pipeline fails the pipeline

## Quote everything

```bash
dir="$1"                          # quote expansions
for f in "$dir"/*.txt; do         # quotes + glob
  cp "$f" "$dest/"
done
```

Unquoted `$var` splits on spaces and globs — the classic source of data loss
(`rm $files` can delete the wrong thing).

## Redirects and files

- Use a temp file + `mv` for atomic writes; never `>` straight onto a file you
  also read in the same script.
- Prefer `mktemp` for scratch files.

## Error handling

```bash
if ! command -v jq >/dev/null 2>&1; then
  echo "jq is required" >&2
  exit 1
fi
```

## Pitfalls

- `set -e` does not always trigger inside `if`/`&&` or on a command whose return
  you're checking — that's expected, not a bug.
- Use `set +e`/`set -e` carefully around commands that legitimately fail.
- Run `shellcheck` on anything non-trivial.

Attached files