How to run Shell With PHP using bash compiler

Asked

Viewed 81 times

0

I’d like to know how to run PHP in the Shell, using the Bash. I tried it this way but make a mistake:

#/usr/bin/php
<?php
echo 'Olá Mundo';

?>

1 answer

2

It is necessary to use #! (with exclamation). This way you will be defining the software that will interpret the written code, for example: #!/usr/bin/php

At the time of execution it is necessary to check the permissions of the file. In order for the code to be executed, the user must have permission to execute. To check the permissions to use ls -l nome-do-arquivo.php

-rwxr-xr-x 1 root root 45 May  3 02:56 ./nome-do-arquivo.php*

It is necessary to have the x. This informs that the file can be executed. If the x do not appear, just run the code below.

sudo chmod +x nome-do-arquivo.php

filename.php

#!/usr/bin/php
<?php
    echo 'Olá Mundo';
?>

/usr/bin/php: bad interpreter: No such file or directory

If you get the above error, just run the following code on the terminal which php. This code will return the path of the PHP interpreter, then just add in #!<caminho informado>

  • i just did that from this error . /agent.php: /usr/bin/php: bad interpreter: No such file or directory

  • @Henriquem I updated the answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.