0

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?

Dom
  • 458
  • 1
  • 5
  • 15

1 Answers1

3

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 '21 at 18:18
  • @Dom: did you add the directory that contains the symlink *before* an old value of PATH: `PATH=:$PATH`? – Arkadiusz Drabczyk Jan 21 '21 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 '21 at 18:27
  • @Dom: and what does `type -a php` say? – Arkadiusz Drabczyk Jan 21 '21 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 '21 at 18:30
  • So no mention of `php is /home//bin/php` - something is wrong. Post output of `ls -Al ~/bin/php` – Arkadiusz Drabczyk Jan 21 '21 at 18:32
  • Home directory for user is set to `/var/www/dom`, so that's the second one. – Dom Jan 21 '21 at 18:33
  • Ok so what does `env php --version` say? Is this the version you want to use? – Arkadiusz Drabczyk Jan 21 '21 at 18:35
  • Says its 7.3, which is where I've symlinked to. The default is 8.0 and that's what I'm getting when I when I run with the shebang. – Dom Jan 21 '21 at 18:36
  • So how are you starting the PHP scripts? Do you use some graphical file manager? Can you run the script in the command line? – Arkadiusz Drabczyk Jan 21 '21 at 18:37
  • I am running the script from the command line in bash. Just doing `./myscript.php` – Dom Jan 21 '21 at 18:38
  • And your shebang is `#!/usr/bin/env php` and you run like that: `./script.php` or provide a full path: `/path/to/script.php`? – Arkadiusz Drabczyk Jan 21 '21 at 18:39
  • I deleted and recreated my script and I'm getting a different result now. I think it may have been working all along and I had a typo in my shebang. So sorry to have wasted your time and thank you for helping. – Dom Jan 21 '21 at 18:42