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
    /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?

  • Short of modifying env from source code, I really don't think that's possible, env is not a trivial shell script and much c processing is involved Commented Mar 20, 2023 at 20:29
  • I've got a bit of a rubbish work around, modify cd, pushd and popd, so that PATH is updated as necessary based on the current directory. Not elegant, but I think it will do.
    – Dom
    Commented Mar 20, 2023 at 20:41
  • Please edit your question and explain what you want to achieve. (Maybe run CGI scripts by different PHP versions depending on their location? What web server do you use?) Whose home directory do you expect ~ to represent when your script is called? This looks like an XY problem.
    – Bodo
    Commented Mar 21, 2023 at 18:33

1 Answer 1


You could write a wrapper for /usr/bin/env that does this:

mv /usr/bin/env /usr/bin/env.bak
cat <<-EOF >/usr/bin/env
    export PATH="${PWD}:${PATH}"
    /usr/bin/env.bak "$@"
chmod 755 /usr/bin/env

every process would use this new env though.

If you want just your shell and its children to see this new env, this question outlines some solutions: Making a process read a different file for the same filename

None of them are perfect though. If you used the accepted solution, a mount namespace, any mounts you made from your shell wouldn't be visible to processes spawned outside your shell.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .