How Drush 9 can use environment variables


Si Hobbs


Working with Drupal sites in Lagoon, and Pantheon, your Drush aliases will follow repeating patterns. Drush 9 supports environment variables that can be exploited for flexible Drush aliases.

Type env on your command line, and you will get a list of environment variables. These have been set by your operating system, your .bash_profile or .bashrc, and other applications which have run in the terminal context.

You can set an environment variable with something like export FAVOURITE_BAND="Ice House" and then print that variable with echo $FAVOURITE_BAND. You don't even have to like Ice House.

With Lagoon and, among others, a local development site will have some contextual information like the current branch (master or feature/method-acting), or a project ID like wabcwuomrnslo on Everything else being equal, these are often the only things that change in your Drush aliases.

(Note that most services have dynamic Drush support, such as platform drush, but this article is not about such things.)

Credit. As usual when it comes to Drush, thanks as always to the Drush docs and examples, and to the support of the maintainers Moshe and Greg in the Drupal Slack.

A drush alias example

Lagoon projects have a project name. Let's say a project is called feraquea and we find this value set in our local development containers via LAGOON_PROJECT. Let's create a reusable drush alias on the host system that can access this value. For this to work it assumes you have ssh key access and you know what the host and port -p values should be.

# drush/sites/
  root: /app
  user: ${env.LAGOON_PROJECT}
    options: -p 1234
    tty: false

The ${env.SOMETHING} is the key here. Any environment variables will be evaluated dynamically. You can see the values applied if you run drush sa.

(If you run this inside a container, the LAGOON_PROJECT will be set, but your private key might not be there. If you run this outside the container, then LAGOON_PROJECT might not be set. I'm not going into that in this post.)

Magic aliases

It's not really an environment variable but while we're on the subject of aliases, this is pretty cool. You can pass the name of your Drush alias into an alias file. Take this example. With an alias file called, you'd normally call on a Drush alias with drush @fish.somesite - where somesite is one of the keys. But if you had a lot of sites or environments, then defining each of these could be time consuming or tedious, depending variously on whether you are a human or a machine. The '*' key changes all this. 


  root: /app
  user: randomproject-${env-name}
  uri: randomproject-${env-name}

The ${env-name} in the above example takes the second part of the alias as an argument. So drush @fish.chips with the above alias file will result in a user called randomproject-chips. Not so random and quite delicious.

A drush.yml example

The use of variables are not limited to aliases. You can use them in your drush.yml file too. This example shows how to use set a default value for the drush site:install command. This assumes you're working in an environment that has set USER to something (assumably your own user name). If you're doing a lot of profile work, installing and reinstalling, and testing over and over, you might stuff like this useful to port your favourite settings between environments.

        account-name: ${env.USER}
        account-pass: "changeme"

Config options

You can also set these options as if they were set as --arguments to drush commands. Setting DRUSH_OPTIONS_URI= is the equivalent of setting --uri=

As you can see, there are quite a lot of options emerging in Drush 9 that can replace what you might have done with PHP in Drush 8 (php based) aliases. If you have any specific questions or issues please ask in the comments below!

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.


  • Allowed HTML tags: <em> <strong> <cite> <blockquote cite> <ul type> <ol start type> <li> <dl> <dt> <dd> <p>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
  • Use [gist:#####] where ##### is your gist number to embed the gist
    You may also include a specific file within a multi-file gist with [gist:####:my_file].

Spread the word