Howtos for The Rest of Us

Skills

Bash Standards

Compatibility and readability conventions for shell scripts.

Shebang

Strict Mode

Start scripts with:

#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'

Style

if [[ -f "$file" ]]; then
    echo "exists"
fi

for item in "${list[@]}"; do
    process "$item"
done

Conditionals

Functions

greet() {
    local name="$1"
    echo "Hello, $name"
}

Error Handling

die() {
    echo "$*" >&2
    exit 1
}
cleanup() {
    rm -f "$tmpfile"
}
trap cleanup EXIT

Input Validation

if [[ $# -lt 1 ]]; then
    die "Usage: $0 <file>"
fi

Output

cat <<EOF > config.ini
[default]
path=$target_dir
mode=0755
EOF

Organization

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

# ── Constants ──────────────────────────────
readonly APP_NAME="my-tool"
readonly CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/$APP_NAME"

# ── Helpers ────────────────────────────────
die() { echo "$*" >&2; exit 1; }

# ── Main ───────────────────────────────────
main() { ... }
main "$@"