Environment Variables Explained

According to Wikipedia, "an environment variable is a dynamic-named value that can affect the way running processes will behave on a computer. They are part of the environment in which a process runs."

This seems pretty straight forward. It's a variable just like any other, and you can use it in the same way. Seems easy!

As a self-taught programmer, though, the first time I ran across environment variables was when I was trying to do something with the Google Maps API in React. I was reading through a tutorial on consuming endpoints, and they mentioned that you shouldn't check API keys into github, and should instead use an environment variable. Sounds great and pretty straightforward. But in practice it took me down a rabbithole of dotenv and random docker docs and ultimately it took me quite a while to get proficient with visualizing exactly how they work, when/why to use them, and especially how to access them across different systems. Talking to a couple of younger programmers, it seems like I'm not alone in this. So let's talk environment variables!

Note that I am on a Mac, and some of this will look slightly different for Windows. They're pretty similar, though.

See the Variables

If you open a terminal and type printenv you'll see a list of all the environment variables currently set on your OS. The list will include things like USER=your_profile_nameand LANG=en_US.UTF-8 if your language is English. Obviously, if you change one of these, it will affect the whole OS. Just like any variable in any program, anywhere. So far so good.

Sometimes you'll want to add your own. This could be because you're working in a virtual environment. It could be because you're working with a Flask application or something similar. It could be because you want to hide your API keys and signing secrets.

Using .env Files

Setting Environment Variables in Heroku

Using Environment Variables in Docker

This is where even my team's intern got a little lost. (Ok, I might have too...)

A docker-compose file can have a section for enviroment variables.