Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Magic Drush Tricks for Your Bash Shell

Parent Feed: 

Drush can do a lot of amazing things that escapes most people’s notice.  For example, did you know that you can change your working directory to a local site by using a site alias?  Just type:

    cdd @mysite

Intriguing? You can make your Drush do this trick with just a little bit of setup.  Drush has always offered a wide range of configuration options, but many Drush users never get deep enough into the documentation to take advantage of these capabilities.  Now, though, it is really easy to get started. In Drush 8.0.0-rc3, there is a new command, core-init (aliased to simply init) to help you get started with a new setup.  To run it, just type:

    drush init

This will print out a list of all of the things that Drush does to set up your configuration for you; one of the things that it does is update your Bash configuration file. It is important to remember, though, that the Bash shell only reads its configuration file when it first starts up. Therefore, after you run drush init, you should also reload your Bash configuration:

    source ~/.bashrc

You have to do this in every open terminal window, but it is only necessary to do this once; if you have too many terminal windows open, you might find it simpler to just restart. Either way, once your Bash configuration has been sourced, you will have a wide range of new shortcuts at your disposal.  For example:

Alias Action
ev ‘return node_load(3);’ Run node_load(3) and print the result.
cc Clear the cache on a Drupal 7 site.
cr Rebuild the cache on a Drupal 8 site.
q 'show tables;' Run an SQL query and print the result.
lsa List all local site aliases.

You can also customize the shortcuts to meet your liking.  Just open up the file ~/.drush/drush.bashrc, and follow the pattern of the aliases that already exist there. Additionally, there is even more customization options available in the file ~/.drush/drushrc.php. Take a look, and see what Drush can do for you!

Topics Development, Drupal Planet, Drupal
Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web