buggedcom
buggedcom

Reputation: 1532

Aliasing a PHP CLI script so that the execution doesn't need PHP prefix

I have a PHP CLI script that I invoke using

php application.php --args etc

However I would like to alias the script so that I can just execute the script without prefixing the command line call with php and having the '.php' extension.

application --args etc

Is this possible? I pressume it is but lack the knowledge or probably the correct terms to search for in Google.

Upvotes: 1

Views: 687

Answers (5)

Spooky
Spooky

Reputation: 1316

Even more logical and pleasant (at least my favorite) call is #!/usr/bin/env php

Quote part from the user contributed note on PHP manual it self:

uses "env" to find where PHP is installed: it might be elsewhere in the $PATH, such as /usr/local/bin.

Upvotes: 0

OnlyAngel
OnlyAngel

Reputation: 157

You need to do the thing that Mike Brants says add the next line to your sample.php file

#!/path/to/cli/php

but also you have to do these in linux

chmod +x sample.php

To tell the linux (unix) machine to interprete these file as an excecutable

Upvotes: 2

arkascha
arkascha

Reputation: 42935

Use a so called 'shebang':

In the first line of your script add:

#!/usr/bin/php

where /usr/bin/php is the path to your php cli executable. That's it !

Upvotes: 0

buggedcom
buggedcom

Reputation: 1532

ahah. alias can be added to the .base_profile

http://www.hypexr.org/bash_tutorial.php#alias

Upvotes: 0

Mike Brant
Mike Brant

Reputation: 71394

You could just use a shebang to define the application to use for execution from within the file. So at the beginning of your script you would place something like this:

#!/path/to/cli/php
<?php
// start your PHP here

When executed from command line the OS will know to use the specified PHP CLI application to execute the script. Obviously the path to the PHP CLI excutable will vary based on your system and should be substituted with what I have shown above.

This is more flexible that aliasing IMO, as you don't need to enter an alias for each PHP script you may want to run in such a manner from the command line.

Upvotes: 1

Related Questions