I run multiple versions of PHP on my Linux server. I've added an alias in my bashrc to point the php command to a specific version alias php='/usr/bin/php7.3'.

This works as expected when I execute a PHP script actually using the php command eg php myscript.php

However, if I make myscript.php an executable and put the shebang #!/usr/bin/env php at the top, it executes the script with the default version of PHP, and does not use the alias defined in my bashrc.

I know I can change the shebang to #!/usr/bin/env /usr/bin/php7.3, but I need this globally, rather than file by file.

Is it possible to set an alias for the php command which is respected in the shebang?

1 Answer 1


Is it possible to set an alias for the php command which is respected in the shebang?

No because shebang is parsed by kernel which doesn't know or care about your shell-specific aliases and environment such as $PATH (this is why you have to provide a full path in the shebang). What you can do is to make php a symlink to the specific php version and prepend the directory that contains it to your PATH so that it will be picked by env.

  • I did as you suggested and whilst it works when running the php command, its not getting picked up by env. $PATH contains the directory with my symlink and running env shows the same path, yet the version of PHP run from the shebang is still the default one.
    – Dom
    Jan 21, 2021 at 18:18
  • @Dom: did you add the directory that contains the symlink before an old value of PATH: PATH=<dir>:$PATH? Jan 21, 2021 at 18:23
  • Yes. PATH in my bashrc looks like this PATH=~/bin:$PATH:~/.composer symlink to required version of PHP created in a bin subdirectory of my home directory.
    – Dom
    Jan 21, 2021 at 18:27
  • @Dom: and what does type -a php say? Jan 21, 2021 at 18:28
  • Four lines: php is aliased to /usr/bin/php7.3', /var/www/dom/bin/php, /usr/bin/php and /bin/php
    – Dom
    Jan 21, 2021 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.