Add a shebang at the first line of your file, something like:
#!/usr/bin/php
Make the file executable:
chmod +x ./ownco.php
Now you can run the script from the command line without specifying the interpreter:
% ./path/to/ownco.php
Ups!
But to avoid having to write the full path to ownco.php
you will need to keep it in a directory that are specified in your $PATH
variable, this usually looks something like this:
% echo "$PATH"
/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
You can see each directory is seperated by :
. You can either move your script to one of these directories or add a new directory to the $PATH
variable. Usually this is done my adding a bit of code to ~/.bashrc
:
export PATH="$PATH:/my/new/place/to/keep/executables"
Some people like to create a bin
directory in their home directory, and keep small script there.
% cat ~/.bashrc
...
...
export PATH="$PATH:$HOME/bin"
...
...
% cat > ~/bin/script1 <<EOL
#!/usr/bin/php
<?php
echo("my first script
");
?>
EOL
% chmod +x ~/bin/script1
% script1
my first script
Notes:
The shebang can be specified on the first line of executable files and start with #!
.
I choose to use /usr/bin/php
as the executable. This is the path for where PHP is stored.
Some people like to use /usr/bin/env php
which will run the version of PHP which are specified in your $PATH
variable. See more