# Creating Environment Variables in Linux

Environment variables let you control how software behaves without modifying the underlying code. Most Linux distributions include a set of predefined environment variables, and you can also define custom variables to store config data, control application behavior, or share values between processes and shell scripts. This guide covers how to view, create, modify, and delete environment variables in Linux, make them persistent across sessions, define global variables for all users, and use them inside shell scripts. By the end, you'll have temporary, persistent, and global environment variables set up and referenced from a working Bash script, following Linux environment configuration practices documented in [**Vultr Docs**](https://docs.vultr.com/how-to-create-environment-variables-in-linux).

---

## 1\. View Available Environment Variables

Linux systems come with several predefined environment variables such as `HOME`, `USER`, `PWD`, and `PATH`.

**1\. List all current environment variables:**

```console
env
```

Output:

```plaintext
PWD=/home/user
HOME=/home/user
USER=user
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
```

You can also use the `printenv` command to display all environment variables.

**2\. Print your current Linux username:**

```console
echo $USER
```

**3\. View an environment variable in key-value format using** `grep`**:**

```console
env | grep USER
```

You can also use `printenv | grep USER` to achieve the same result.

## 2\. Create Temporary Environment Variables

To define a temporary environment variable in Linux, write the variable name (key) in uppercase and assign it a string value. Temporary environment variables remain active only within the current terminal session and do not persist after you close the shell.

**1\. Define a new environment variable named** `GREETING`**:**

```console
GREETING=Hello
```

Enclose the value in double quotes if it includes spaces, for example:

```console
GREETING="Hello World"
```

**2\. Export the variable so that child processes can access it:**

```console
export GREETING
```

**3\. Verify that the variable is set:**

```console
echo $GREETING
```

Output:

```plaintext
Hello
```

Temporary environment variables only last for the duration of the current shell session. Once you close the terminal, they're removed automatically.

## 3\. Persist Environment Variables

To retain environment variables across terminal sessions, define them in your shell's configuration file, such as `~/.bashrc` or `~/.bash_profile` if you're using the Bash shell. This ensures the variable loads every time you start a new session.

**1\. Open your** `~/.bashrc` **file in a text editor:**

```console
nano ~/.bashrc
```

**2\. Add the following line to the bottom of the file:**

```console
export GREETING=Hello
```

Save and close the file.

**3\. Reload the shell configuration to apply the changes:**

```console
source ~/.bashrc
```

**4\. Confirm that the environment variable is now available:**

```console
echo $GREETING
```

Output:

```plaintext
Hello
```

**5\. Open a new terminal window and verify the variable persists:**

```console
echo $GREETING
```

## 4\. Modify Environment Variables

You can update environment variables either temporarily or permanently. Not all environment variables are intended to be changed — system-defined variables often control critical operations and should only be modified with caution. Always ensure you have the proper permissions and understand the implications before making changes.

**1\. Update the** `GREETING` **variable value in your current terminal session:**

```console
export GREETING=Hi
```

**2\. Confirm the new value:**

```console
echo $GREETING
```

Output:

```plaintext
Hi
```

**3\. Open your shell configuration file to persist the change across sessions:**

```console
nano ~/.bashrc
```

**4\. Modify the value for the** `GREETING` **definition:**

```console
export GREETING=Hi
```

**5\. Reload the file to apply the changes:**

```console
source ~/.bashrc
```

## 5\. Remove Environment Variables

To remove an environment variable, use the `unset` command. If you defined the variable in your shell configuration file, delete the definition there to prevent it from being reloaded in future sessions.

**1\. Use** `unset` **to remove the** `GREETING` **environment variable:**

```console
unset GREETING
```

**2\. Verify that the variable has been removed:**

```console
echo $GREETING
```

If no output is returned, the variable has been successfully removed.

`unset` only removes variables from the current session. If the variable is defined in your shell configuration file (such as `~/.bashrc`), it will reappear in new sessions unless you also delete the definition from the file.

## 6\. Create Global Environment Variables

By default, environment variables apply only to the user who created them. If you want to define environment variables that are accessible system-wide, for all users and environments, add them to the `/etc/environment` file. This file is parsed during login and is suitable for defining global values without shell-specific syntax.

**1\. Open the** `/etc/environment` **file using Nano:**

```console
sudo nano /etc/environment
```

Only users with root privileges can edit `/etc/environment`, as changes affect all users system-wide. Avoid storing passwords or secrets in global variables, since they're readable by all users.

**2\. Add your variable without the** `export` **keyword:**

```console
GLOBAL_GREETING=Hello
```

Do not include `export`. The [pam_env module](https://man7.org/linux/man-pages/man8/pam_env.8.html), which parses this file, does not support shell syntax or commands like `export`. Save and close the file.

**3\. Log out and log back in to reload the environment.** The system only reads `/etc/environment` during the login process (via SSH, terminal, or GUI).

**4\. Run the** `env` **command to confirm that the variable is available:**

```console
env
```

Output:

```plaintext
GLOBAL_GREETING=Hello
```

**5\. Create a new user:**

```console
sudo adduser newuser
```

**6\. Switch to the new user:**

```console
su - newuser
```

Enter the password when prompted.

**7\. Verify that the global environment variable is available to the new user:**

```console
echo $GLOBAL_GREETING
```

Output:

```plaintext
Hello
```

To remove the variable, delete its entry from `/etc/environment` and log out and back in again.

## 7\. Reference Environment Variables in a Bash Script

You can dynamically reference environment variables — whether user-defined or global — inside Bash scripts. The steps below show how to use the global `GLOBAL_GREETING` variable within a simple script.

**1\. Create a new script file named** `greeting.sh`**:**

```console
nano greeting.sh
```

**2\. Add the following script to reference the environment variable:**

```bash
#!/bin/bash
echo "${GLOBAL_GREETING}! Welcome to your server."
```

Use curly braces (`{}`) to clearly define the variable boundary when appending special characters or strings. For example, the exclamation mark (`!`) follows the variable name without ambiguity. Save and close the file.

**3\. Make the script executable:**

```console
chmod +x greeting.sh
```

**4\. Run the script:**

```console
./greeting.sh
```

Output:

```plaintext
Hello! Welcome to your server.
```

You can also nest variables inside other variable assignments. For instance, `GREETING="Hi! $USER"` expands `$USER` when executed. However, this type of expansion only works when the variable is defined in a shell configuration file like `.bashrc`, not in `/etc/environment`.

## Next Steps

- Use environment variables to externalize secrets and config in your own scripts and services.
- Audit `/etc/environment` and `.bashrc` periodically to avoid variable sprawl.
- Combine environment variables with a `.env` file and a loader for application-specific configuration.
- Explore `direnv` for per-directory environment variable management.

For the full guide with additional tips, visit the original article on [**Vultr Docs**](https://docs.vultr.com/how-to-create-environment-variables-in-linux).
