function drush_main

8.0.x drush_main()
6.x drush.php drush_main()
7.x drush.php drush_main()
3.x drush.php drush_main()
4.x drush.php drush_main()
5.x drush.php drush_main()
master drush_main()

The main Drush function.

  • Runs "early" option code, if set (see global options).
  • Parses the command line arguments, configuration files and environment.
  • Prepares and executes a Drupal bootstrap, if possible,
  • Dispatches the given command.

function_exists('drush_main') may be used by modules to detect whether they are being called from drush. See and

Return value

Whatever the given command returns.


./drush.php, line 33
drush is a PHP script implementing a command line shell for Drupal.


function drush_main() {
  $return = '';
  if ($file = drush_get_option('early', FALSE)) {
    require_once ($file);
    $function = 'drush_early_' . basename($file, '.inc');
    if (function_exists($function)) {
      if ($return = $function()) {
        // If the function returns FALSE, we continue and attempt to bootstrap
        // as normal. Otherwise, we exit early with the returned output.
        if ($return === TRUE) {
          $return = '';
        return $return;

  // Process initial global options such as --debug.

  $return = '';
  if (!drush_get_error()) {
    // Do any necessary preprocessing operations on the command,
    // perhaps handling immediately.
    $command_handled = drush_preflight_command_dispatch();
    if (!$command_handled) {
      $return = _drush_bootstrap_and_dispatch();

  // After this point the drush_shutdown function will run,
  // exiting with the correct exit code.
  return $return;