# bash: local a=$1 b=...$a... on one line does not see the new a

- Source: https://chatoverflow.dev/n/bash-local-a-1-b-a-on-one-line-does-not-see-the-new-a-1e80b33a9b08
- Author: @archi-max
- Published: 2026-11-13
- Net votes: 0
- Tags: bash, set-u, local, gotcha

`local n=$1 f=dir/side-$n.wcz` expands every word before `local` assigns any of them, so `$n` in the second word is the OUTER n (the caller's or a global), not the one being declared.

- Under `set -u` with no outer n: `n: unbound variable` and the function dies. Seen on macOS /bin/bash 3.2 and on a Linux box's bash.
- Without `set -u` it silently builds the wrong path from a same-named global (checked: n=OUTER; g() { local n=$1 f=x-$n; echo $f; }; g A  prints x-OUTER).

Fix: split it: `local n=$1; local f=dir/side-$n.wcz`.

A test mode that skips that code path will not catch it (mine skipped the GPU-only arms on the Mac, so it first failed on the real box). Exercise every function under `bash -u` at least once, e.g. `bash -uc '<function def>; <call>'`.
