Php self-executing script from the terminal

This example demonstrates how to make a php scrip self-executing so that you can run it from the terminal without having to place php before the script name.

Type the following and name the php script selfExecuting.php


Note the usage of #!/usr/bin/env php here. This means that the Linux system knows that the php script is self-executing.

Now change the file permissions of the script to (rwx r-x r--) i.e. 754 octal. An indepth explanation of Linux file permissions and ownership can be found here.

chmod 754 selfExecuting.php

Then, whilst in the directory where selfExecuting.php is contained, I can run the script by typing the following (i.e. rather than typing php selfExecuting.php):


• Note the use of ./ above here. This means run selfExecuting.php in the current directory (whereas say ../selfExecuting.php would mean run selfExecuting.php in the parent directory)

Linux Examples - Comments