Creating Custom Drush Commands

Creating a new drush command is very easy. There are four simple steps.

  1. Create a command file called
  2. Implement the function COMMANDFILE_drush_command()
  3. Implement the functions that your commands will call. These will usually be named drush_COMMANDFILE_COMMANDNAME().

For an example drush command, see examples/ The steps for implementing your command are explained in more detail below.


The name of your drush command is very important. It must end in "" to be recognized as a drush command. The part of the filename that comes before the "" becomes the name of the commandfile. Your commandfile name is used by drush to compose the names of the functions it will call, so choose wisely.

The example drush command, 'make-me-a-sandwich', is stored in the 'sandwich' commandfile, ''. You can find this file in the 'examples' directory in the drush distribution.

Drush searches for commandfiles in the following locations:

Note that modules in the current Drupal installation will only be considered if drush has bootstrapped to at least the DRUSH_BOOSTRAP_SITE level. Usually, when working with a Drupal site, drush will bootstrap to DRUSH_BOOTSTRAP_FULL; in this case, only the drush commandfiles in enabled modules will be considered eligible for loading. If drush only bootstraps to DRUSH_BOOTSTRAP_SITE, though, then all drush commandfiles will be considered, whether the module is enabled or not. See `drush topic docs-bootstrap` for more information on bootstrapping.

Additionally, drush commandfiles may optionally define a function COMMANDFILE_drush_load() in the file If this function returns FALSE, then the commandfile will not be loaded.

Implement COMMANDFILE_drush_command()

The drush_command hook is the most important part of the commandfile. It returns an array of items that define how your commands should be called, and how they work. Drush commands are very similar to the Drupal menu system. The elements that can appear in a drush command definition are shown below.

The 'sandwich' drush_command hook looks like this:

        function sandwich_drush_command() {
          $items = array();

          $items['make-me-a-sandwich'] = array(
            'description' => "Makes a delicious sandwich.",
            'arguments' => array(
              'filling' => 'The type of the sandwich (turkey, cheese, etc.)',
            'options' => array(
              'spreads' => 'Comma delimited list of spreads (e.g. mayonnaise, mustard)',
            'examples' => array(
              'drush mmas turkey --spreads=ketchup,mustard' => 'Make a terrible-tasting sandwich that is lacking in pickles.',
            'aliases' => array('mmas'),
            'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap at all.

          return $items;

Most of the items in the 'make-me-a-sandwich' command definition have no effect on execution, and are used only by `drush help`. The exceptions are 'aliases' (described above) and 'bootstrap'. As previously mentioned, `drush topic docs-bootstrap` explains the drush bootstrapping process in detail.


The 'make-me-a-sandwich' command in is defined as follows:

	function drush_sandwich_make_me_a_sandwich($filling = 'ascii') {
	  ... implementation here ...

If a user runs `drush make-me-a-sandwich` with no command line arguments, then drush will call drush_sandwich_make_me_a_sandwich() with no function parameters; in this case, $filling will take on the provided default value, 'ascii'. (If there is no default value provided, then the variable will be NULL, and a warning will be printed.) Running `drush make-me-a-sandwich ham` will cause drush to call drush_sandwich_make_me_a_sandwich('ham'). In the same way, commands that take two command line arguments can simply define two functional parameters, and a command that takes a variable number of command line arguments can use the standard php function func_get_args() to get them all in an array for easy processing.

It is also very easy to query the command options using the function drush_get_option(). For example, in the drush_sandwich_make_me_a_sandwich() function, the --spreads option is retrieved as follows:

        $str_spreads = '';
        if ($spreads = drush_get_option('spreads')) {
          $list = implode(' and ', explode(',', $spreads));
          $str_spreads = ' with just a dash of ' . $list;

Note that drush will actually call a sequence of functions before and after your drush command function. One of these hooks is the "validate" hook. The 'sandwich' commandfile provides a validate hook for the 'make-me-a-sandwich' command:

        function drush_sandwich_make_me_a_sandwich_validate() {
          $name = posix_getpwuid(posix_geteuid());
          if ($name['name'] !== 'root') {
            return drush_set_error('MAKE_IT_YOUSELF', dt('What? Make your own sandwich.'));

The validate function should call drush_set_error and return its result if the command cannot be validated for some reason. See `drush topic docs-policy` for more information on defining policy functions with validate hooks, and `drush topic docs-api` for information on how the command hook process works. Also, the list of defined drush error codes can be found in `drush topic docs-errorcodes`.

To see the full implementation of the sample 'make-me-a-sandwich' command, see `drush topic docs-examplecommand`.