Bash Coding Standards
April 24, 2026 · View on GitHub
These standards apply to ALL bash scripts in the tfenv repository. Read this entire document BEFORE writing or modifying any bash code.
Mandatory Pre-Write Verification
Before writing ANY bash code, verify you understand these rules. Violations caught after writing waste time — get it right first.
Shell Setup
Every script in libexec/ contains its own boilerplate for resolving
TFENV_ROOT and sourcing helpers. This is intentional — each script
must be executable in isolation. Do NOT refactor this into a shared loader.
#!/usr/bin/env bash
set -uo pipefail;
- Shebang:
#!/usr/bin/env bash(exact format) - Strict mode:
set -uo pipefail;— NEVER useset -e - The project does NOT use shellcheck — this is a deliberate choice
Indentation and Formatting
- 2-space indentation throughout (no tabs)
- Terminate ALL statements with a semicolon (
;) where syntactically correct - Include vim modeline at end of new scripts:
# vim: set syntax=bash tabstop=2 softtabstop=2 shiftwidth=2 expandtab smarttab :
Variables
- ALL variable references use braces:
${variable}(never$variable) - Script-local variables use lowercase:
${version},${remote} - Environment variables use UPPERCASE:
${TFENV_ROOT},${TFENV_ARCH} - Always quote variable expansions:
"${variable}" - Quote entire strings containing variables:
"${dir}/file"NOT"${dir}"/file - Use
${var:-default}for optional variables (scripts run withset -u)
Quoting
- Single-quote strings unless variable expansion is needed
- Double-quote strings that contain variable expansion
- NEVER use unescaped
!inside double-quoted strings (bash history expansion)
Command Substitution
- Use
$(...)NOT backticks
Error Handling
- NEVER use
set -e— handle errors explicitly - Use the project's existing error patterns:
error_and_diefor fatal errors (available vialib/helpers.sh)- Inline
||pattern for simple cases:some_command || error_and_die "Failed to do thing";
- Note:
error_and_diedoes NOT exist in the test context — tests useerror_and_proceedinstead
Functions
- Define as:
function name() { ... }; - Use
localfor variables inside functions
Cross-Platform Considerations
- macOS uses BSD
sed/grep/readlink— GNU extensions may not be available - The project installs
ggrepon macOS but uses systemsedandreadlink .terraform-versionfiles may contain\rfrom Windows/WSL — always strip withtr -d '\r'after reading
Common Pitfalls
- Shell operator precedence:
cmd1 || cmd2 | cmd3meanscmd1 || (cmd2 | cmd3), not(cmd1 || cmd2) | cmd3 - Unquoted variables: Always quote
"${var}"— unquoted variables cause word-splitting - Double-quoted traps:
trap "rm ${var}" EXITexpands at definition time. Use functions or single quotes. $@in for-loops: Always quote:for arg in "$@"- Regex anchoring:
^1.1matches1.10.xbecause.is a regex wildcard. Use^1\.1\.for exact prefix matching.
Pre-Submission Checklist
-
#!/usr/bin/env bashshebang -
set -uo pipefail;(notset -e) - 2-space indentation, no tabs
- All variables use
${braces} - All variable expansions double-quoted
- All statements terminated with
; - No unescaped
!in double-quoted strings -
$(...)for command substitution (no backticks) -
localused inside functions - Cross-platform compatibility considered