SH

Shell Defaults

Core Shell/Bash coding conventions

Details

Language / Topic
shellShell/Bash
Category
Style Guide

Rules

balanced
- Always start scripts with `#!/usr/bin/env bash` and `set -euo pipefail` to catch errors, undefined variables, and pipe failures early.
- Use `snake_case` for all variable and function names; declare local variables in functions with `local` to prevent scope leakage.
- Quote all variable expansions as `"$var"` and `"${array[@]}"` to prevent word splitting and glob expansion on unexpected values.
- Use `$(command)` for command substitution instead of backticks — backticks do not nest and are harder to read.
- Declare constants with `readonly VAR=value` at the top of the script — uppercase names (`MAX_RETRIES=3`) signal that a value must not change.
- Use `[[ ... ]]` for conditionals instead of `[ ... ]` — it handles empty strings, regex matching (`=~`), and quoting more predictably.
- Group related functions together and place them before `main()`; call `main "$@"` at the end of every script.
- Prefer `printf '%s\n' "$value"` over `echo "$value"` for reliable output — `echo` behavior with flags varies across shells.