Skip to content
Back to skills

Bash Patterns

ASecurity

Write, review, and fix Bash (.sh) scripts using project conventions and best practices. Always use this skill when writing any .sh file, creating a new shell script, fixing a bash error or runtime error, reviewing shell code, adding a new script to scripts/, or when the user asks about bash style, naming, formatting, or patterns. This skill combines official style rules with project-specific patterns to produce correct, portable scripts on the first try. Trigger on: write bash, create .sh, ba...

  • 3 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added October 6, 2026
code-qualityshellbashgit

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned November 9, 2026

npx -y skills add e128/dotnet-reference --skill bash-patterns --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Bash Patterns?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Bash Patterns
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/e128-bash-patterns/badge)](https://www.skillsdirectory.com/skills/e128-bash-patterns)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: bash-patterns
description: >
  Write, review, and fix Bash (.sh) scripts using project conventions and best practices.
  Always use this skill when writing any .sh file, creating a new shell script, fixing a
  bash error or runtime error, reviewing shell code, adding a new script to scripts/, or
  when the user asks about bash style, naming, formatting, or patterns. This skill combines
  official style rules with project-specific patterns to produce correct, portable scripts
  on the first try. Trigger on: write bash, create .sh, bash script, shell script, bash error,
  fix .sh, bash style, bash patterns, script error in bash.
---

# Bash Scripting Patterns

Bash 4.0 minimum (5.3 current). `scripts/lib.sh` re-execs under Homebrew Bash. Scripts live in `scripts/` (`.sh`), must pass `shellcheck` before commit.

## Naming Conventions

| Element | Convention | Example |
|---------|-----------|---------|
| Script files | kebab-case | `build-project.sh` |
| Functions | snake_case | `function fetch_user()` |
| Local variables | snake_case | `local user_id` |
| Constants | SCREAMING_SNAKE_CASE | `readonly OUTPUT_DIR` |
| Environment variables | SCREAMING_SNAKE_CASE | `$APP_VERSION` |

## Script Header Template

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

# One-line summary of what this script does.
#
# Usage:
#   scripts/my-script.sh [--flag] <arg>
```

**Always start with `set -euo pipefail`.**

## Formatting Rules

- No trailing whitespace.
- Indent with 2 spaces (not tabs).
- Lines <= 100 chars preferred; wrap long lines with `\`.
- Quote all variable expansions: `"$var"` not `$var`.
- Use `[[ ]]` for conditionals (not `[ ]`).
- Use `$(command)` for subshells (not backticks).

## Argument Parsing Template

```bash
SOLUTION=""
FLAG=false
JSON=false

while [[ $# -gt 0 ]]; do
  case "$1" in
    --flag)    FLAG=true; shift ;;
    --json)    JSON=true; shift ;;
    -*)        echo "Unknown flag: $1" >&2; exit 1 ;;
    *)         SOLUTION="$1"; shift ;;
  esac
done

[[ -z "$SOLUTION" ]] && { echo "Usage: script.sh <arg> [--flag]" >&2; exit 1; }
```

## Core Patterns

### JSON Output Mode

Use `jq` for JSON construction and parsing:
```bash
if $JSON; then
  jq -nc \
    --arg status "$status" \
    --argjson count "$count" \
    '{status: $status, count: $count}'
fi
```

### Error Handling

```bash
# Trap for cleanup
cleanup() { rm -f "$TMP_FILE"; }
trap cleanup EXIT

# Check command exists
command -v dotnet &>/dev/null || { echo "dotnet not found" >&2; exit 1; }

# Capture exit code without exiting
output=$(some_command 2>&1) || true
exit_code=$?
```

### Temporary Files

```bash
TMP_FILE="$(mktemp)"
trap 'rm -f "$TMP_FILE"' EXIT
```

### Prerequisites Check

```bash
check_prerequisites() {
  local -a missing=()
  for tool in git dotnet jq; do
    command -v "$tool" &>/dev/null || missing+=("$tool")
  done
  if [[ ${#missing[@]} -gt 0 ]]; then
    echo "Missing required tools: ${missing[*]}" >&2
    exit 1
  fi
}
```

### Shared Library

Source `scripts/lib.sh` for shared helpers:

```bash
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
```

It provides `info`, `ok`, `warn`, `err`, `find_repo_root`, `has_flag`, `get_option`, `json_object`, and `iso_timestamp`. Read `lib.sh` before you add a helper.

## Command Design Rules

- Max 2 positional parameters; use `--flags` for anything beyond that.
- Document every exported function with a comment line above it.
- Provide both long and short flag forms for commonly-used flags (`-j` / `--json`).
- Validate external tool availability at script start (see Prerequisites pattern).
- Always use `local` for function-scoped variables.
- Use `readonly` for constants that should not change.

## Pitfalls Quick Reference

| Symptom | Root cause | Fix |
|---------|-----------|-----|
| `unbound variable` | `set -u` + unset var | Use `${var:-}` for optional vars |
| Script exits silently | `set -e` + failed command | Add `|| true` for expected failures |
| `command not found` | Missing PATH or wrong name | Check `command -v` first |
| Glob expansion in variable | Unquoted `$var` with `*` | Always quote: `"$var"` |
| Pipe swallows exit code | Default pipe behavior | `set -o pipefail` (in header) |
| Array empty check fails | Wrong syntax | Use `${#arr[@]}` not `$arr` |
| Heredoc indentation breaks | Spaces in heredoc | Use `<<-` with tabs, or `<<'EOF'` |
| Word splitting in loop | Unquoted expansion | Quote arrays: `"${arr[@]}"` |
| Subshell variable lost | `( )` creates subshell | Use `{ }` or process substitution |

## Test Before Commit

`scripts/lint-shell.sh` is the authoritative shell linter. It runs
`shellcheck -S warning` and fails on any warning. Fix every finding before you
commit.

```bash
scripts/lint-shell.sh scripts/my-script.sh
```

## Self-Improvement

After writing or fixing a Bash script, add a row to the Pitfalls Quick Reference (Symptom | Root cause | Fix) only when the issue would affect most future scripts. State the rule, not the incident or date. Correct any instruction in this skill that contradicted what worked.

Files in this skill

  • SKILL.md5.5 KB
  • references/bash-reference.md5 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in code-quality →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.