How to add shebang #! with php script on linux?

孤街醉人 提交于 2019-12-04 23:49:46

It should (for most systems) be #!/usr/bin/env php, but your error isn't related to that.

-bash: script.php: command not found

It says that script.php is not found.

If the problem was the shebang line then the error would say something like:

bash: script.php: /usr/env: bad interpreter: No such file or directory

Presumably, you are typing script.php and the file is not in a directory on your $PATH or is not executable.

  1. Make it executable: chmod +x script.php.
  2. Type the path to it instead of just the filename, if it is in the current directory then: ./script.php.

Instead of 2, you can move/copy/symlink the file to somewhere listed in $PATH or modify the $PATH to include the directory containing the script.

If you script is not located in your /usr/local/bin and is executable, you have to prefix calling your script with php like this:

php myscrip.php

For shebangs, here is what I use:

Like this:

#!/usr/bin/php

or this:

#!/usr/bin/env php

Leaving here some little notes:


To use a php binary located inside the same folder.

As example a php7.2 executable copied from /usr/bin is in the same path along a hello script.

#!./php7.2
<?php

echo "Hello!"; 

To run it:

./hello

Which behave just as equal as:

./php7.2 hello

This give portability, but beware of system architectures, the php binary might not match the target platform.


Setting allowed memory from the hashbang:

We can set one INI entry from the hashbang line:

#!/usr/bin/php -d memory_limit=2048M
<?php
phpinfo();
exit;

Then to see if php had understood, using phpinfo():

./myphpProg | grep memory

Correct shell output should contain:

memory_limit => 2048M => 2048M

Doing the above is similar as this command line:

php -d memory_limit=2048M myphpProg.**php**

This is why we can set only one ini value in hashbangs, as php accept only one -d parameter at a time.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!