Environment Variables: A Comprehensive Guide
Tags: beginners, tutorial, productivity, codenewbie
Published: 2023-09-16 08:24:47

Even as beginner developers or software engineers, chances are you’ve already stumbled upon **environment variables.**

This article sums it up just as much as you need to use it in your coding journey.

Let’s dive in.

**What are environment variables?**

Environment variables are dynamic named values that affect the way running processes behave on a computer system. They are stored in a special system area called the environment, and they can be used by any program that needs them.

Environment variables can be used for a variety of purposes, such as:

- Specifying the location of executable files
- Setting the default language and locale
- Configuring database connections
- Enabling or disabling program features
- Storing secret keys and other sensitive information

**How to set environment variables**

The specific steps for setting environment variables vary depending on your operating system.

**Windows**

To set an environment variable in Windows:

1. Open the **Control Panel**.
2. Click **System and Security**.
3. Click **System**.
4. Click **Advanced system settings**.
5. In the **Environment Variables** section, click **New** under **User variables for**.
6. Enter a name for the environment variable and a value for it.
7. Click **OK** to save the changes.

**Linux**

To set an environment variable in Linux:

- Open a terminal window.
- Type the following command:

`export VAR_NAME="Hello World"`

Replace `VAR_NAME` with the name of the environment variable and `Hello World` with its value.

- Press `Enter` to save the changes.

**Mac**

To set an environment variable in Mac:

- Open a terminal window.
- Type the following command:

`export VAR_NAME="Hello World"`

Replace `VAR_NAME` with the name of the environment variable and `Hello World` with its value.

- Press `Enter` to save the changes.

**Temporary and permanent environment variables**

You can set environment variables to be either temporary or permanent.

A **temporary environment variable** is only available for the current session. Once you close the terminal window or command prompt, the variable will be deleted.

A **permanent environment variable** is available for all future sessions. To set a permanent environment variable, you need to edit a configuration file.

**Windows**

To set a permanent environment variable in Windows:

1. Open the **Control Panel**.
2. Click **System and Security**.
3. Click **System**.
4. Click **Advanced system settings**.
5. In the **Environment Variables** section, click **Edit** under **User variables for**.
6. Select the environment variable you want to edit and click **Edit**.
7. Enter a new value for the environment variable.
8. Click **OK** to save the changes.

**Linux**

To set a permanent environment variable in Linux:

- Open a terminal window.
- Edit the `.bashrc` file:

`nano ~/.bashrc`

- Add the following line to the end of the file:

`export VAR_NAME="Hello World"`

Replace `VAR_NAME` with the name of the environment variable and `Hello World` with its value.

- Save and close the file.

**Mac**

To set a permanent environment variable in Mac:

- Open a terminal window.
- Edit the `.bash_profile` file:

`nano ~/.bash_profile`

- Add the following line to the end of the file:

`export VAR_NAME="Hello World"`

Replace `VAR_NAME` with the name of the environment variable and `Hello World` with its value.

- Save and close the file.

**Practical use cases**

Here are some practical use cases for environment variables:

- **Docker:** Environment variables can be used to pass configuration settings to Docker containers. For example, you could set an environment variable to specify the database connection string or the API key for a third-party service.
- **OpenAI API key:** To use the OpenAI API, you need to set a `OPENAI_API_KEY` environment variable. You can do this in the same way as setting any other environment variable.
E.g. to set OpenAI API Key as environment variable inside your terminal, simply pass the following and hit enter:
    
    ```bash
    export OPENAI_API_KEY=<Your API Key>
    ```
    
- **Node.js:** Environment variables can be used to configure Node.js applications. For example, you could set an environment variable to specify the port that the application should listen on.
- **Sensitive Information:** Environment variable is super important to avoid hardcoding your sensitive information into the code you're going to push in remote repository. Cough, cough: GitHub, GitLab, BitBucket, Codeberg and etc pp. You get the gist.

**Conclusion**

Typically, environment variables are used to set up variables like passwords for our application to integrate with. It's an alternative way of transmitting local configuration data to the container in a format that our application can read from.

90% of the time when you need to declare environment variable for those sweet API Token, you’ll provably need to declare it in the working directory terminal. Environment variables are a powerful tool that can be used to configure your system and applications. By understanding how to set and use environment variables, you can make your life easier and improve your productivity.

Hope this helps and enjoy your finesse coding journey.

Let’s connect on [𝕏](https://twitter.com/hafeezhaqq).

Should you need any help or improvement suggestion, just comment down below.

We’ll figure it out together.

Cheers,

Hafeez Pizofreude