3

I like to have drush command 'go_to' that will move my shell into different directory. My custom implementation does not move the shell directory. How come? here is the script so far:

#!/usr/bin/env drush
<?php

$dir = '/dir/to/move/my/shell';
drush_op('chdir', $dir);
drush_op_system('cd '.$dir);
//drush_shell_exec('cd '.$dir);
//print_r(drush_shell_exec_output());

Thank you for help

5

The "cd" command in your commandfile is working correctly; however, it is a standard behavior for *nix (and Windows) that environment variables, such as the current working directory variable set by the 'cd' command apply only to the process that they are set in. Drush runs in a separate process from the shell that you launch it from, so your current working directory will revert to its original value when Drush terminates. (It actually never changed, since each process has its own environment.)

Instead of using a Drush alias (site alias or shell alias), what you might actually prefer is a bash alias. For example:

$ alias go='cd /dir/to/move/my/shell'
$ go
$ pwd
/dir/to/move/my/shell

There's a lot more you can do here if you define site aliases for your site, and source the example bashrc file that Drush provides; see the Drush README file, and the examples folder.

If you want to write fancy php code change your working directory, you need to put only the code that calculates the destination path, and then call 'cd' from a shell alais. For example (from bash):

$ alias go='cd $(drush dd @mysite)'
$ go
$ pwd
/path/to/drupal/root/of/mysite

This one liner will also do the trick:

$ cd `drush @mysite dd`

zsh aliases have the same syntax as bash aliases:

http://zsh.sourceforge.net/Intro/intro_8.html  

It also looks like process substitution has the same syntax in zsh as well:

http://zsh.sourceforge.net/Intro/intro_7.html#SEC7

However, I haven't tried any of this in zsh.

  • Thank You Greg. i have been using zsh alias, however. It doesn't provide the freedom and ease that I can have doing things in php code something that Drush provides. Is there a way i can run in the same process? Thanks for the great explanation and another good learning. You are awesome:) – latvian Sep 4 '14 at 20:41
  • Unfortunately, all shell scripts, including php programs such as Drush, are always launched in a separate process, and there is no way for a child process to affect the environment of the parent process. You need to run code in the shell in order to affect the shell's environment. That means that if you want to do convenient things in php, you need to write a php command to print out the directory you want to go to, and then call it from a shell alias. See my updated answer above. – greg_1_anderson Sep 5 '14 at 15:40
1

You might want to check out aliases:

http://befused.com/drupal/drush-site-aliases

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.