--- title: Environment Variables date: 2022-08-13 19:25:00 -0400 categories: [tech, programming] tags: [variables, linux] --- # The Environment Variables *❝ Don’t bury your burden in saintly silence. You have a problem? Great. Rejoice, dive in, and investigate. ❞* *— Ven. Henepola Gunaratana* ## What are environment variables? Variables and Constants are fundamental components of computer programming languages. Both represent unique memory locations containing data that programs use in their computations. Unlike constants, variables may change during execution. An environment variable is then a variable whose value is set outside the program and may change. Ordinarily, they are set through a functionality built into the operating system or microservice. An environment variable comprises a name/value pair, as in program-defined variables. Any number may be created and available for reference at a point in time. Environment variables affect how a process–an instance of a running program–behaves on a computer. Running processes query the value of the environment variables to determine a suitable location and or path to the location owned by the user running the process. #### Types of env. variables Two types of environment variables exist: 1. *user environment variables* and, 2. *system environment variables*. **User environment variables**, as the name suggests, are environment variables that are specific to each user account. This type of env. variables are user-defined and are temporal unless added to the scripts that hold system environment variables. .bashrc for Linux and environment registry for windows. **System environment variables** extend beyond just one user, applying to any user that might exist, or is created in the future. You might also think of system environment variables as ‘built-in’ variables, defaulted in operating systems. Most system environment variables point to important locations. The most well-known environment variable is probably `PATH` which contains the paths to all folders that might contain executables. With `PATH`, you can write just the name of an executable rather than the full path to it in your terminal since the shell will check the local directory as well as all directories specified in the `PATH` variable for this executable. ## When and where are environment variables useful? Before we know how to set, check for and use environment variables, let us have a look at why and where we use them. As mentioned earlier, environment variables are set outside the program code, and this makes them safer to be used as API access tokens. When used this way you don’t have to worry about accidentally pushing sensitive credentials to a place such as GitHub. Differentiating between production and development environments. Another most common use case for env. variables is in the configuration options for different environments. When developing software and other services, we want to have a development version or sandbox available to make test requests against, that way it doesn't impact real production data. Environment variables are helpful because they allow us to change which of our environments use which third-party service environment by changing an API key, endpoint, or whatever the service uses to distinguish between environments. What if we are hosting our application in a cloud environment such as Heroku, Azure, or AWS or even wrapping it in a Docker container? All of these providers support ways to define environment variables, that are used to connect the local environment to the environment that these services run in. In summary, environment variables are to be used when a value/data varies from environment to environment and their abstraction from the program code would in a way make the data safer to use. The variables include but are not limited to data such as: domain names, API URLs/URIs, public and private authentication keys (only secure in server applications), and group mail addresses; such as those for marketing, support, sales, etc. ## How are they set? ### Setting up environment variables in Windows Setting up environment variables in Windows is easy and fast. There are different ways to do that. GUI-based methods differ slightly for different Windows versions, although the command line methods remain unchanged for all Windows versions. We’ll use the command prompt method to set up our environment variables. Open the command prompt. Use the following syntax and press Enter: `setx [variable_name] “[variable_value]”` `variable_name` stands for the name of the variable you want to enter. `variable_value` stands for the value for the newly created variable. For example, let’s create a “what” with the value “it’s all in windows” and then verify its existence using Command Prompt. We use the following command: `setx [what] “[it’s all in windows]”` ![](../assets/22-14-08/export-windows.png "Setting env variables in Windows.") **Congratulations!** :smile: We’ve just created a new user environment variable using Command Prompt. Now, let’s verify its existence. We use the set command, which lists all environment variables, including the one we have set. If the variable we have just set is not displayed in that shell instance, close the window and run set in a new one. After all, it's all in windows); ![](../assets/22-14-08/check-windows.png "Checking exixtence of env variable.") ### Setting environment variables in Linux/macOS To set environment variables on macOS or any UNIX-based operating system, you first have to figure out which shell you are running. You can do that by running in your terminal the command: `echo $SHELL` or `echo $0` ![](../assets/22-14-08/echo-shell.png "Which shell is running?") The output should indicate which shell you are running. Most times the shell would be the bash shell. Other shells include `<zsh>` or `<fish>`. [image] To set an environment variable we use the export command in the following format: `Export KEY="value"` And that’s done. Our environment variable is set. But wait, the environment variable has only been set for our current session. If we’d want to have it in all our sessions, we’d have to add that line in the **.bashrc/.zshrc** file. To test that our environment variable has been set successfully, we use the echo command together with the expansion character($) and the KEY given to the variable. The following example shows the setting of an environment variable and the querying of the variable. ![](../assets/22-14-08/export-linux.png "setting env variable in linux.") ### How to use env variables in code? Different Programming languages have different modules to access environment variables in your machine and use them in their programs. In this article, we’ll use the `os` module in python3 to retrieve an environment variable and possibly manipulate it. The `os` module contains a plethora of functions to get information on — and in some cases, to manipulate — local directories, files, processes, and environment variables. ![](../assets/22-14-08/python3.png "getting env variables in python3") ## Wrapping it up *“Abstraction brings the world into more complex, variable relations; it can extract beauty, alternative topographies, ugliness, and intense actualities from seeming nothingness.” — Jerry Saltz*` Using environment variables is one technique to make our apps easier to configure by separating infrequently changing data from our code. But as simple as this technique may be, its use is influenced by considerations such as the application type (frontend or backend) and the operating environment (operating system or microservice). Exploiting environment variables is easy, but understanding their nuances and being able to efficiently and securely utilize them is one factor that sets experienced Developers apart from inexperienced Developers. **As with any technology, the trick isn’t knowing how to use something, it’s knowing when to use it.** If you have any questions or would like to show me the awesome thing you are currently building, feel free to send me an **Email** at: themadbit@duck.com or contact me on **Twitter** [@themadbit](https://twitter.com/themadbit). I can’t wait to see what you are building. ## References. 1. [Diveintopython3](https://diveintopython3.net/) 1. [Wikipedia](https://en.wikipedia.org/) 1. [Twilio](https://www.twilio.com/)