Environment Variables

Have you ever been inside a Ruby file and seen a line that looks something like this:

ENV[‘FOO’] = bar

and wondered to yourself, what exactly is going on here? Let’s try some things together and see if we can’t create some new understanding. The ENV is short for environment. This environment includes all of the information that is available to us when the console environment is loaded, and depends on some configuration files like your .bashrc or .zshrc to be set up completely. For example, if you are using Zsh you can open up your terminal window and type the command:

vim .zshrc

to bring up your “runtime configuration” or “resource control” file for Zsh. In this file I needed to add a line so that my programs could access my file path. This line looks something like this:

export PATH=”/usr/local/bin . . .”

The export command sets the next word to be an environment variable. The environment variable name is usually all uppercase, but can also be lowercase. The value of the variable must be a string, thus there must be quotes around it. To see what environment variables are available in your environment, open your terminal and enter the command:

env

Environment variables can also be defined on the fly. Enter the command:

export FOO=”bar”

The next time you print your environment variables, the last line on the list will say FOO=bar. Fire up your interactive Ruby REPL:

irb

and try this command:

ENV[‘FOO’]

What do you see? You should see something like:

“bar”

From this you have now demonstrated that environment variables, when set outside of a running program, can be accessed within that program so long as it is run in the same environment. The Ruby syntax for accessing environment variables is ENV followed by an array operator with a string of that variable’s name as the key.

One useful application of this concept is in the deployment of an open source app using basic authorization when you want to keep the username and password combination secret. For example, here is an unsafe way of publishing code for a deployed app. And here is a gist of a safer way. In order for the second way to work you must set the environment variables APP_USERNAME and APP_PASSWORD, as demonstrated above, before launching the app. This way, your secrets are kept secret, and anyone else who wants to launch the app on their own can set their own username and password environment variables.

There are many other ways in which environment variables can be handy, and this short demo has just scratched the surface. For further exploration on the subject, check out these sources:

Setting Environment Variables in Heroku
Unix Environment Variable Lesson
Unix Environment Variable Tutorial