PATH, startup and environment safety
An executable name without a slash is resolved using shell lookup rules and PATH. An empty or relative PATH entry can depend on the current directory. In sensitive scripts, use a deployment-approved absolute path or a controlled PATH containing only trusted directories; verify ownership and permissions during deployment. A command found by command -v is not automatically trustworthy. Bash can execute noninteractive initialization selected by BASH_ENV before the script body runs, so sanitizing it inside that body is too late for that invocation. Launch from an already trusted parent with an explicit environment, for example GNU env -i PATH=/usr/bin:/bin LC_ALL=C /bin/bash --noprofile --norc .... Adapt absolute paths to the platform. This suppresses inherited variables for the child but is not isolation, does not verify system binaries and does not repair an already compromised parent. Shell options, functions, locale and CDPATH can also influence results. A script should set only necessary environment values and avoid exporting secrets. The fixture creates a new private tool directory and checks its creation and permissions before execution. The absolute path identifies the intended fixture; a bare name can still be shadowed by a shell function.
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.
Choose a controlled executable
umask 077mkdir trusted || exit 1printf '#!/bin/bash\nprintf "approved\\n"\n' > trusted/bmtool || exit 1chmod 700 trusted/bmtool || exit 1PATH="$PWD/trusted:/usr/bin:/bin" bmtool || exit 1"$PWD/trusted/bmtool"Expected stdout:
approvedapprovedBoth calls select the fixture tool in an owned directory. No real system executable is replaced.
Start with a narrow environment
export BM_DEMO=visible/bin/bash --noprofile --norc -c 'printf "%s\n" "$BM_DEMO"' || exit 1/usr/bin/env -i PATH=/usr/bin:/bin LC_ALL=C /bin/bash --noprofile --norc -c 'printf "%s\n" "${BM_DEMO-unset}"' Expected stdout:
visibleunsetThe second child does not inherit BM_DEMO.
Use a trusted startup fixture
printf 'printf "startup\\n"\n' > startup.bash || exit 1BASH_ENV="$PWD/startup.bash" /bin/bash --norc -c 'printf "body\n"' || exit 1/usr/bin/env -i PATH=/usr/bin:/bin LC_ALL=C /bin/bash --noprofile --norc -c 'printf "clean\n"' Expected stdout:
startupbodycleanThe first child executes the authored startup file; the clean child receives no BASH_ENV.
Common mistakes
--norc alone does not suppress BASH_ENV. A clean environment does not enforce filesystem or network isolation.
Practice
Use two disposable directories containing harmless tools that print distinct labels. Predict lookup with each controlled PATH. Explain why a verified deployment path matters.
Next: Safe temporary files and filesystem races