BASH_ENV and startup safety
An ordinary noninteractive Bash process can execute a startup file named by the inherited BASH_ENV variable before its script starts. Bash expands the value and does not search PATH for the resulting filename. Treat it as executable configuration, not passive data. The file may change variables, functions, options and output. --noprofile --norc alone does not suppress BASH_ENV. For controlled execution, construct an explicit environment before starting Bash, with known PATH, HOME and locale, and omit untrusted BASH_ENV, ENV, exported functions and inherited shell options. This reduces surprises but does not isolate arbitrary code. Never source a file supplied by an untrusted party; validate provenance and permissions. In POSIX interactive startup, ENV has a different role. Use absolute paths to trusted files and do not test against actual personal configuration. Check creation of each startup fixture before invoking a child. The file is executed in the child shell; an export inside that file does not change the parent environment.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code.
Load a trusted startup fixture
printf '%s\n' 'export BM_LABEL=fixture' > startup.bash || exit 1BASH_ENV="$PWD/startup.bash" bash --noprofile --norc -c 'printf "%s\n" "$BM_LABEL"' Expected stdout:
fixtureThe startup file executes before the command string.
Construct a clean child environment
printf '%s\n' 'BM_LABEL=fixture' > startup.bash || exit 1export BASH_ENV="$PWD/startup.bash"env -i PATH=/usr/bin:/bin HOME="$PWD" LC_ALL=C /bin/bash --noprofile --norc -c 'printf "%s\n" "${BM_LABEL-unset}"' Expected stdout:
unsetThe explicit environment omits BASH_ENV, so the file is not selected.
BASH_ENV is not searched through PATH
mkdir configs || exit 1printf '%s\n' 'BM_LABEL=found' > configs/startup.bash || exit 1PATH="$PWD/configs:$PATH" BASH_ENV=startup.bash bash --noprofile --norc -c 'printf "%s\n" "${BM_LABEL-unset}"'BASH_ENV="$PWD/configs/startup.bash" bash --noprofile --norc -c 'printf "%s\n" "$BM_LABEL"' Expected stdout:
unsetfoundThe relative name does not locate the file in configs through PATH; the absolute filename works.
Common mistakes
Do not treat BASH_ENV as an ordinary data file or assume --norc disables it. Do not inherit an uncontrolled environment in a test runner.
Practice
Create a synthetic startup file exporting a harmless label. Compare a child with BASH_ENV set and a clean env -i child. Verify the parent label remains unset.
Next: Aliases and prompt configuration