Skip to content

nullglob bad-example leaks temp dir under Bash 3.2 (EXIT trap in subshell) #13

Description

@posidoni

What's wrong

examples/bash/04-nullglob-empty-match.bad.sh defines mktemp -d and
trap 'rm -rf "$tmp"' EXIT inside the main() ( ... ) subshell:

main() (
  tmp=$(mktemp -d)
  trap 'rm -rf "$tmp"' EXIT
  cd "$tmp"

  for file in *.txt; do
    printf 'found: %s\n' "$file"
  done
)

Under Bash 3.2 — which macOS ships by default and which this repo's
shell-macos CI job explicitly targets — an EXIT trap defined inside a
subshell (...) does not fire when the subshell exits. The temp directory
is created but never cleaned up, leaking on every run (including every CI run
of tests/examples.bats on the macOS job).

Flagged by gemini-code-assist on PR #12, and already fixed on the paired
.good.sh in the same PR (see the merged commit) by hoisting tmp/trap to
global scope:

tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT

main() (
  cd "$tmp"
  ...
)

What to do

Apply the identical hoist to examples/bash/04-nullglob-empty-match.bad.sh
so both files in the pair use the same (correct) temp-dir/trap scope. No
behavioral change to the demonstrated pitfall (the unmatched-glob-stays-literal
behavior is unaffected) — this is purely a cleanup/portability fix.

Verify

task ci

must stay green.

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions