32

I'm having a little issue with adding shebang #! with my php script on RedHat linux. I have a small piece of test code with shebang added (I've tried different variations as well), but I get the following error message everytime I try to run the script.

Error msg:

-bash: script.php: command not found

Test script:

#!/bin/env php    
<?php echo "test"; ?>

Shebang #! variations:

#!/usr/bin/php
#!/usr/bin/env php
user2799603
  • 873
  • 3
  • 13
  • 19
  • 1
    It's obvious - that's why he is asking :) – Banago Feb 12 '14 at 15:13
  • @JoelFan the PHP documentation seems to disagree with you: http://www.php.net/manual/en/features.commandline.usage.php#example-375 – Digital Chris Feb 12 '14 at 15:17
  • 1
    @JoelFan - people have the freedom to express their silly thoughts on blogs, don't do it on a site where we help other developers. Thank you. To answer the question about the error - it's most likely because of CR\LF combination that comes after your first line. Use hex editor to check whether you have \n\r and if yes, remove carriage return. CHMOD +x yourfile.php to make it executable too. – N.B. Feb 12 '14 at 15:17
  • offtopic, should be moved to shell usage – user3694243 May 24 '19 at 10:56

5 Answers5

51

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
17

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
Banago
  • 1,350
  • 13
  • 22
5

In reply to @NVRM's comment regarding only single use of -d, this is not true.

Start with a chmod +x script as

#!/usr/bin/php
<?php
phpinfo();

and run script | grep -E 'memory_limit|error_reporting', and you'll see

error_reporting => no value => no value
memory_limit => 128M => 128M

Now add some -d entries so you have

#!/usr/bin/php -d memory_limit=2G -d error_reporting=-1
<?php
phpinfo();

and re-run script | grep -E 'memory_limit|error_reporting', and you'll now see

error_reporting => -1 => -1
memory_limit => 2G => 2G

Thus demonstrating you can set multiple options.

In fact, the entire command line is what you are working with here. So you can load extensions, use a different config, etc., everything you can do at the command line.

Richard A Quadling
  • 3,769
  • 30
  • 40
0

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.

NVRM
  • 11,480
  • 1
  • 88
  • 87
0

find callable shebang for PHP in Linux, Don't memorize this it, learn how to use it


which php

output


zeus@pop-os:~$ which php
/usr/bin/php



then shebang must be

#!/usr/bin/php  
dılo sürücü
  • 3,821
  • 1
  • 26
  • 28