function _drush_backend_generate_command

8.0.x _drush_backend_generate_command($site_record, $command, $args = array(), $command_options = array(), $backend_options = array())
6.x _drush_backend_generate_command($site_record, $command, $args = array(), $command_options = array(), $backend_options = array())
7.x _drush_backend_generate_command($site_record, $command, $args = array(), $command_options = array(), $backend_options = array())
3.x _drush_backend_generate_command($command, $args, &$data, $method = 'GET', $drush_path = null, $hostname = null, $username = null, $ssh_options = NULL)
4.x _drush_backend_generate_command($command, $args, &$data, $method = 'GET', $drush_path = null, $hostname = null, $username = null, $ssh_options = NULL)
5.x _drush_backend_generate_command($site_record, $command, $args = array(), $command_options = array(), $backend_options = array())
master _drush_backend_generate_command($site_record, $command, $args = array(), $command_options = array(), $backend_options = array())

Generate a command to execute.


site_record: An array containing information used to generate the command. 'remote-host' Optional. A remote host to execute the drush command on. 'remote-user' Optional. Defaults to the current user. If you specify this, you can choose which module to send. 'ssh-options' Optional. Defaults to "-o PasswordAuthentication=no" 'path-aliases' Optional; contains paths to folders and executables useful to the command. '%drush-script' Optional. Defaults to the current drush.php file on the local machine, and to simply 'drush' (the drush script in the current PATH) on remote servers. You may also specify a different drush.php script explicitly. You will need to set this when calling drush on a remote server if 'drush' is not in the PATH on that machine.

command: A defined drush command such as 'cron', 'status' or any of the available ones such as 'drush pm'.

args: An array of arguments for the command.

command_options: Optional. An array containing options to pass to the remote script. Array items with a numeric key are treated as optional arguments to the command. This parameter is a reference, as any options that have been represented as either an option, or an argument will be removed. This allows you to pass the left over options as a JSON encoded string, without duplicating data.

backend_options: Optional. An array of options for the invocation. @see drush_backend_invoke for documentation.

Return value

A text string representing a fully escaped command.

2 calls to _drush_backend_generate_command()
drush_backend_invoke_concurrent in includes/
Execute a new local or remote command in a new process.
drush_sql_sync in commands/sql/


includes/, line 1089
Drush backend API


function _drush_backend_generate_command($site_record, $command, $args = array(), $command_options = array(), $backend_options = array()) {
  $drush_path = null;
  $php = '';

  $site_record += array(
    'remote-host' => NULL,
    'remote-user' => NULL,
    'ssh-options' => NULL,
    'path-aliases' => array(),

  $hostname = $site_record['remote-host'];
  $username = $site_record['remote-user'];
  $ssh_options = $site_record['ssh-options'];
  $os = drush_os($site_record);

  if (drush_is_local_host($hostname)) {
    $hostname = null;

  foreach ($command_options as $key => $arg) {
    if (is_numeric($key)) {
      $args[] = $arg;

  $cmd[] = $command;
  foreach ($args as $arg) {
    $cmd[] = drush_escapeshellarg($arg, $os);
  $option_str = _drush_backend_argument_string($command_options, $os);
  if (!empty($option_str)) {
    $cmd[] = " " . $option_str;
  $command = implode(' ', array_filter($cmd, 'strlen'));
  if (!is_null($hostname)) {
    if (drush_is_windows($os)) {
      if (!is_null($username)) {
        $username = " -u:" . drush_escapeshellarg($username, "LOCAL");
        if (array_key_exists('winrs-password', $site_record)) {
          $username .= " -p:" . drush_escapeshellarg($site_record['winrs-password'], "LOCAL");
      $command = "winrs" . $username . " -r:" . drush_escapeshellarg($hostname, "LOCAL") . " " . drush_escapeshellarg($command, "LOCAL");
    else {
      $username = (!is_null($username)) ? drush_escapeshellarg($username, "LOCAL") . "@" : '';
      $ssh_options = $site_record['ssh-options'];
      $ssh_options = (!is_null($ssh_options)) ? $ssh_options : drush_get_option('ssh-options', "-o PasswordAuthentication=no");

      $ssh_cmd[] = "ssh";
      $ssh_cmd[] = $ssh_options;
      $ssh_cmd[] = $username . drush_escapeshellarg($hostname, "LOCAL");
      $ssh_cmd[] = drush_escapeshellarg($command . ' 2>&1', "LOCAL");

      // Remove NULLs and separate with spaces
      $command = implode(' ', array_filter($ssh_cmd, 'strlen'));

  return $command;