Transform your Custom PHP Script into a Globally Available CLI Command
August 01, 2016

This is a little tip to help programmers and system admins shave off a few milliseconds from their daily command line interface workflow. For the uninitiated, please note that some programmers, including yours truly, have been reported to spend hours or even days trying to automate a repetitive task that takes 5 seconds to execute. Yes, this will pay off in 25 years. Thank you.

Anyways, you have a helper PHP script under Linux that you frequently use on your local dev machine or server and you usually call it with:

php /home/user/scripts/adjutant.php make-me-a-sandwich

Now, ideally, you'd like to shave off as much characters as possible from that command and run it as below from any currently active directory:

adjutant make-me-a-sandwich

Here's how. First option is to create an executable shell script that calls your PHP script and pass the command line arguments to it. Then you add this new script to your path environment variable so that it's callable from any folder you're in. Below is a skeleton of such a script and further discussions on how to accomplish this here, here, and here.

#!/bin/sh 
php /home/user/scripts/adjutant.php "$@"

Your second, and recommended, option is to use Bash shell aliases. Basically, a Bash alias is just a keyboard shortcut to any command you wish. The cherry on top is that command line arguments will automatically be propagated to your script as well.

There are many ways to create Bash aliases depending on how your system is setup. However, the easiest and most common way is to just edit your ".bashrc" file found in your home directory and add the following line to it:

alias adjutant='php /home/user/scripts/adjutant.php'

An even better organized solution would be to create the ".bash_aliases" file and put all your custom aliases in it then import it from ".bashrc". The import is already done for you on the latest versions of Ubuntu and all you need to do is just create and setup the ".bash_aliases" file.

Now just restart your terminal and enjoy running your command with a shortcut. And finally, when all else fails, just try:

sudo adjutant make-me-a-sandwich

Pun intended: StarCraft's Adjutants and XKCD's Make me a sandwich.

By the way, "sudo" won't work as the alias is setup to run on your user profile and "sudo" breaks that.

Happy command bashing.


Development PHP CLI

Share this post


Written by
Mario Awad

Founder of SOFTKUBE, lead developer, and getting things done addict. Passionate about open source, user interface design, business development, and the tech world.

More about Mario Awad


About
SOFTKUBE

A small team of experts developing simple, usable, and high-quality web solutions. We blog about business, entrepreneurship, web development, and technology.

More about us


Recent Posts

Custom Theme Migration from Drupal 9 to Drupal 10

How to Change the Most Recent Git Commit Message

How to Make Google Chrome Forget a Permanent HTTP 301 Redirect

Finding and Fixing Unintended Body Overflow to Remove Horizontal Scrollbars

View all posts


All Posts Categories

Business Cheat Sheets CLI Design Development Downloads Drupal Email Google Apps HID Keyboards Multilingualism Open Source Philosophy PHP Pointing Devices Productivity Quotes Science Security SEO Technology Thoughts Windows Zend Framework