Posted by & filed under PHP.

Normally, to run a PHP script, you type php ./script-name.php, or more conveniently, php script-name.php.

If you want to make a php script executable, e.g. by typing ./script-name.php, you’ll want to add the following line to the top of the document:

#!/usr/bin/env php

Yes, that goes before the <?php statement. You’ll also want to let your *nix environment know that the file is executable. You can do this by typing:

chmod u+x filename.php

One of the cool things about *nix land is that file extensions don’t matter, so you can rename the file and leave off the .php part. Then your script will work by typing ./script-name.

But, we can do better. Perhaps you don’t want to type the ./ part either. To fix this, you need to add the script to your PATH. Your PATH is a set of directories which your shell environment looks in to find executable files.

To see what your path is, run:

echo $PATH

Which, on my system, tells me /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin (since I’m in OS X).

What you can do is make a bin (or hidden .bin) directory in your home and place your various command line scripts in there, then add that directory to your path. You can do all of these tasks by running the following commands:

echo "PATH=\$HOME/bin:\$PATH" >> ~/.bash_profile
source ~/.bash_profile

From now on, any scripts you have in your ~/bin directory will be executed with a higher priority than existing scripts (if there is a name conflict). So, you can now run your PHP script by typing “script-name” from anywhere.

Bonus: If you are in OS X, and use Finder, and don’t want that ugly bin directory showing up all the time, type the following:

chflags hidden ~/bin

Thomas Hunter II

Thomas is passionate about technology and building products. A web design business created while attending college slowly evolved into a brick and mortar on Main St. of his small Midwestern hometown. His desire for fame and fortune led to the co-founding of a Y Combinator startup and a life in California.


Hey there! I'm currently writing a book on Microservices which I expect to release in early 2017. If you're interested in getting updates please signup here. More info about the Book