0

Well i want create command and register it on Linux/Mac/Windows For example, i want use $ ownco create project hello

But ownc is php script like this(ownco.php or ownco):

<?php
if (count($argv) > 3) {
    echo 'Hi';
} else {
    echo 'Ups!';
}

I dont want use php ownco ... Thanks!

Olaf Erlandsen
  • 5,817
  • 9
  • 41
  • 73

1 Answers1

1

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\n");
?>
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

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123