I am trying to write a .bashrc file such that files using the #!/usr/bin/env shebang will have their PATH adjusted based on the directory in which they reside.
I've got something towards what I want with the following in my ~/.bashrc:
function env() {
case $PWD/ in
/var/www/php73/*)
PATH=~/bin73;;
/var/www/php8.1/*)
PATH=~/bin81;;
esac
/usr/bin/env "$@"
}
Then if I call env from /var/www/php73 or any subdirectory I get ~/bin73 included in the path as I want.
The problem is that this doesn't work for files which use the shebang #!/usr/bin/env as .bashrc is only defining a function for env, not the env program with its full path.
Is it possible to get .bashrc to modify PATH when the shebang is used? And if so, how?
envfrom source code, I really don't think that's possible,envis not a trivial shell script and much c processing is involved – Nicolas Formichella Mar 20 '23 at 20:29cd,pushdandpopd, so thatPATHis updated as necessary based on the current directory. Not elegant, but I think it will do. – Dom Mar 20 '23 at 20:41~to represent when your script is called? This looks like an XY problem. – Bodo Mar 21 '23 at 18:33