Skip to content

Fix #532: terraform shim fails with "@: unbound variable" on Bash 3.2 when run with no arguments - #541

Open
marcleblanc2 wants to merge 1 commit into
tfutils:masterfrom
marcleblanc2:fix/exec-no-args-bash3
Open

marcleblanc2 wants to merge 1 commit into
tfutils:masterfrom
marcleblanc2:fix/exec-no-args-bash3

Conversation

@marcleblanc2

@marcleblanc2 marcleblanc2 commented Sep 29, 2026 •

Copy link
Copy Markdown

Description

Under set -u, Bash 3.2 (macOS /bin/bash) treats an empty "$@" as an unbound variable, so running the shim with no arguments aborts before terraform runs:

/opt/homebrew/Cellar/tfenv/3.2.2/lib/tfenv-exec.sh: line 31: @: unbound variable

0bd45d1 changed the argument loop in tfenv-exec() to for _arg in "${@}" to stop word-splitting arguments with spaces (#453).

zsh's bashcompinit invokes complete -C commands with no arguments (it only sets COMP_LINE/COMP_POINT), so on macOS every tab completion after terraform prints only this error.

The fix uses for _arg; do, which iterates the positional parameters without expanding "$@". It neither word-splits (keeps #453 fixed) nor trips set -u on any Bash version, and it is POSIX.

Also adds test/test_exec.sh for the shim and corrects the for-loop guidance in AGENTS.md / .github/instructions/bash.instructions.md, which previously recommended the form that caused this regression.

Issue Link

Fixes #532

Testing

  • Ran ./test/run.sh locally; commands, env_vars, exec, install_lock, list, list_remote, pin, resolve_version, symlink, uninstall, use_minrequired, version_file pass. install_and_use, prerelease_filter, use_dash, use_latestallowed fail identically on unmodified master on this machine with Bad CPU type in executable (they install pre-1.0 darwin_amd64 builds; Apple Silicon without Rosetta), so unrelated to this change.
  • Tested on at least one platform (note which below)
  • New tests added: test/test_exec.sh
  • Real-world repro: COMP_LINE="terraform pl" COMP_POINT=12 bin/terraform prints plan with this branch; the 3.2.2 Homebrew shim prints the unbound-variable error.

Platform tested: macOS 26 (arm64), Bash 3.2.57

Quality Checklist

  • Shell quoting verified — all variables quoted, braces used
  • Cross-platform considered — BSD vs GNU tool differences (/bin/bash test iteration is skipped where absent)
  • No unintended changes to other commands
  • README.md updated — not user-facing docs
  • CHANGELOG.md updated — left for the release process, per AGENTS.md

…ash 3.2 when run with no arguments

0bd45d1 changed the argument loop in tfenv-exec() to `for _arg in "${@}"`
to stop word-splitting arguments with spaces (tfutils#453). Under `set -u`,
Bash 3.2 (macOS /bin/bash) treats an empty "$@" as an unbound variable,
so running the shim with no arguments now aborts before terraform runs.

zsh's bashcompinit invokes `complete -C` commands with no arguments, so
every tab completion after `terraform` printed only this error.

Use `for _arg; do`, which iterates the positional parameters without
expanding "$@", so it neither word-splits nor trips `set -u` on any
Bash version.

Add test/test_exec.sh covering the shim with no arguments (under both
`bash` and /bin/bash) and a -chdir path containing a space, and correct
the for-loop guidance in AGENTS.md and bash.instructions.md.

Amp-Thread-ID: https://ampcode.com/threads/T-01a0eee4-d46a-776d-9873-341f0d5f7fd2
Co-authored-by: Amp <amp@ampcode.com>
@marcleblanc2

Copy link
Copy Markdown
Author

Hey @Zordrak, is this project active and / or looking for maintainers? It's been a game changer to use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

/opt/homebrew/Cellar/tfenv/3.2.2/lib/tfenv-exec.sh: line 31: @: unbound variable

1 participant