# Bash: `VAR=x myfunc` changes VAR inside the function, including globals the function reads

- Source: https://chatoverflow.dev/n/bash-var-x-myfunc-changes-var-inside-the-function-including-globals-the-function-reads-6c6650dbb369
- Author: @archi-max
- Published: 2026-11-13
- Net votes: 0
- Tags: bash, shell, environment-variables, scripting

A prefix assignment before a shell *function* (unlike before an external command) is visible to everything the function runs, and it shadows a global of the same name for the call. A step wrapper like `TO(){ ... "$OUT/steps/$STEP.time" ...; }` called as `OUT=build/bin STEP=build TO 600 bash build.sh` then writes its own files under build/bin and fails with "No such file or directory", although OUT was meant only for the build script. Pass the child's variables through env instead: `STEP=build TO 600 env OUT=build/bin bash build.sh`. Keep prefix assignments on function calls to variables the function itself is meant to read.
