Search code examples
phpshellshebang

PHP Shebang not working


NOTE: This question has been solved. It is a duplicate of shebang line not working


This is what I have:

#!/usr/bin/env php
<?php
echo "starting";

The file is executable "-rwxr-xr-x"

I call the file with its complete path

/var/projects/user-backup.php

But I only get the error-message ": No such file or directory"

Calling the script with

php /var/projects/user-backup.php

results in the String "Starting..." being displayed as expected.

Even when I try to

 /usr/bin/env php /var/projects/user-backup.php

everything works out fine, the String "starting..." is displayed as expected.

Where am I going wrong?

EDIT1: Using shebang like this:

#!/usr/bin/php

results in the Error-message "^M: bad interpreter: No such file or directory" although there definitely is a /usr/bin/php (/usr/bin/php -> /etc/alternatives/php , and /etc/alternatives/php -> /usr/bin/php5)


Solution

  • I think I've got it (due to question @EDIT1).

    A couple of words about [Wikipedia]: Newline (EOLNs):

    • Win: "\r\n" ("\x0D\x0A", CR + LF)

    • Nix:

      • Linux: "\n" ("\x0A", LF)

      • OSX: "\r" ("\x0D", CR)

    Your .php file has Win style EOLNs (at least the one on the shebang line), while some Nix interpreters don't like CR, and this is the case here.

    As a side note, Nano (or any decent editor, as a matter of fact) figures out the line endings in a file when editing it, and when adding new lines, their EOLNs are consistent with the existing ones.

    To make things work, convert your file EOLNs to Nix (Linux) style:

    dos2unix /var/projects/user-backup.php
    

    Might want to also read [SO]: Flask CLI throws 'OSError: [Errno 8] Exec format error' when run through docker-compose (@CristiFati's answer).