Setting environment variables on your machine is important in many cases if you’re a software developer. I’ve written an article that discusses the advantages of environment variables, albeit in the context of Flask. In this article, I’ll provide a step-by-step guide on creating your own environment variables for use within your applications.
What are environment variables?
Think of environment variables as variables set outside of the program. These are variables set on the system that a program runs on and can be accessed within a program. It might also help to think about them as superglobal variables. If we think of global variables within a program as ones that can be used throughout the program, environment variables can be used throughout the system.
Why use environment variables?
There are many reasons to use environment variables. These include:
- Decoupling application configuration from program logic. Setting different values for variables in different environments and then simply reading those environment variables in our logic allows us to deploy our application in multiple environments with different configurations without worrying about changing our code.
- Keeping sensitive information secret. Often we create programs that use sensitive information such as secret keys and passwords. Using environment variables allows us to keep this sensitive information away from the source code. This is especially important for open source projects.
- Variables do not have to be re-declared in every new program. If for some reason, you have a variable that is shared between multiple programs, such as an email address, for example, you can define it as an environment variable and simply read it whenever it’s needed in a particular program.
Setting environment variables
Now that we’ve discussed why they’re important, let’s get to the point of this article. How do we set these environment variables? Note that this tutorial is carried out on macOS Mojave (version 10.14.6).
There are 2 types of variables we can set:
These variables can be set directly from the terminal. They are only temporary, meaning they are erased as soon as the terminal window is closed. They are also local to that terminal window. Other terminal windows will not be able to read them. Let’s set one up as an example. Open a terminal window and set a variable called “name” to any value:
$ export name="Kelvin Mwinuka" $ echo $name Kelvin Mwinuka
Here, we’ve set a variable called “name” to “Kelvin Mwinuka”. The echo keyword simply prints the argument it’s given. Make sure to include the “$” symbol in front of the variable name.
As I’ve stated before, this variable will only live in this terminal window for the duration of the window’s life. So why is it useful? Well, one good reason for using this type of variable is if you’re running multiple terminal prompts that use the same information. Instead of typing the value every time it’s needed, set it on the first prompt and reference it in future prompts.
If you’re still reading this article, it’s probably because you want to know how we can make the environment variables persist throughout the system, even once the system has been rebooted.
This is a little more complex than the previous method but is fairly simple once you grasp it. In your user directory, there’s a file named “.bash_profile”. This file contains system configurations (i.e environment variables). This is where we will define our environment variables for use throughout our system.
The bash_profile file is hidden by default. So we’ll need the terminal to access it. Navigate to your user home if you’re not already in the directory. Once there you can run the following command:
This will display a list of all the files and folders in the directory. However, notice that we don’t see any of the hidden files, which is what we’re interested in. In order to display all files, hidden or not, type the following command:
$ ls -la
The terminal should now display all the files in the directory. There are 2 files that we’re interested in here: “.bash_profile” and “.bash_profile.backup”. The latter might be named slightly different on your system but it should still be clear that it’s a backup of bash_profile. If you don’t have this backup file, then create it with the following command:
$ touch .bash_profile.backup
Before we edit bash_profile, we have to make a backup first. We can do this by copying all the contents of bash_profile to bash_profile.backup using the following command:
$ cp .bash_profile .bash_profile.backup
Once we’ve made the backup, we can go ahead and create our variables. Open bash_profile using your favourite text editor. For me, that’s atom so I can open the file with the following command:
$ atom .bash_profile
Your command might look different depending on your editor of choice.
Now that the file is open, we can now set our environment variables. Add these to the end of the file. Here’s an example demonstrating how I set the website URL and email address for kelvinmwinuka.com:
WEBSITE="https://www.kelvinmwinuka.com" export WEBSITE WEBSITE_EMAIL="firstname.lastname@example.org" export EMAIL
We have created 2 variables here: WEBSITE and WEBSITE_EMAIL. Make sure there are no spaces between the variable name, equals sign, and the value. after you set the variable name, making it accessible is as easy as using the “export” keyword followed by the variable name. Save the file and close it.
That’s it! Now we can use the variable anywhere we please. These variables will persist even between system reboots. Let’s create a python script that prints these environment variables to the console:
import os print(os.environ.get('WEBSITE')) print(os.environ.get('WEBSITE_EMAIL'))
Save the script and run it. You should see the following output:
You can use the variables in the terminal too:
$ echo $WEBSITE "https://kelvinmwinuka.com" $ echo $WEBSITE_EMAIL "email@example.com"
The caveat with the process we’ve just completed is that these will only work in our current user profile because this is where we’ve been operating for. However, this shouldn’t be an issue for most applications you develop as they will run in the same context.
It is my hope that by this point, you’ve understood the basics of environment variables and are capable of implementing them. Be sure to use these in your projects as they open up new possibilities in your code implementations.
Enjoyed the article or found it useful? Help out with a share and spread the word to other developers! Also feel free to drop a comment below. I’d love to hear your thoughts
I write awesome articles regularly. Subscribe so you don’t miss out on great content!