Managing Environment Variables in Linux

Environment variables provide a flexible way to influence how software behaves without needing to modify any source code. Most Linux distributions include several predefined environment variables, and you can also create custom ones to store configuration data, control software behavior, or share values across processes and shell scripts.

This guide shows you how to view, define, edit, and remove environment variables on Linux. You’ll also learn how to make variables persistent between sessions, set global variables for all users, and use them within shell scripts.

View Existing Environment Variables

Linux systems automatically define a number of environment variables like HOME, USER, PWD, and PATH. Follow these steps to inspect them directly from your terminal.

List All Current Environment Variables

Output:

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

You can also run the printenv command to show all defined environment variables.

Display a Single Environment Variable

Run the following command to print your current username.

Search for a Specific Variable

To view a specific variable in key-value format, use grep as shown below:

You can achieve the same result using printenv | grep USER.

Create Temporary Environment Variables

To set up a temporary environment variable in Linux, write the variable name (usually in uppercase) followed by an equals sign and its value. Temporary variables exist only within the active terminal session—they are lost once you close the shell.

Define a New Temporary Variable

If your variable’s value contains spaces, enclose it in quotes:

Export the Variable

To make the variable available to child processes, export it:

Verify the Variable

Output:

Temporary variables remain active only for the current shell session. Once the terminal is closed, they are automatically discarded.

Make Environment Variables Persistent

To keep environment variables available across terminal sessions, define them in your shell configuration file, such as ~/.bashrc or ~/.bash_profile (for Bash). This ensures they load every time a new shell session begins.

Edit the Configuration File

Add the Variable Definition

Save the file and close the editor.

Apply the Changes

Verify the Persistent Variable

Output:

Open a new terminal session and confirm that the variable persists:

Modify Environment Variables

You can modify environment variables temporarily or permanently. However, take caution: some system-defined variables are crucial for system stability and should not be changed unless necessary. Ensure you have sufficient permissions and understand the effects of your changes.

Temporarily Update a Variable

To update the value of the GREETING variable within the current session:

Check the new value:

Output:

Persist the Updated Variable

To make the change permanent, open your shell configuration file again:

Update the variable definition as follows:

Reload the file to apply your changes:

Remove Environment Variables

To delete an environment variable, use the unset command. If the variable is defined inside your shell configuration file, make sure to remove its definition there as well to prevent it from reloading in future sessions.

Delete a Variable from the Current Session

Verify Removal

If the command produces no output, the variable has been successfully deleted.

Remember, unset removes variables only from the active shell session. If a variable is defined in your configuration file (for example, ~/.bashrc), it will return in future sessions unless you also remove its line from that file.

Create Global Environment Variables

By default, environment variables apply only to the user who created them. To define variables available to all users and processes across the system, add them to the /etc/environment file. This file is processed during user login and uses a simple key-value format without shell-specific syntax.

Edit the Global Environment File

$ sudo nano /etc/environment

Only users with administrative privileges can modify /etc/environment because it affects every user account on the system. Avoid storing sensitive data such as passwords or secrets in this file, as it’s readable by all users.

Add a Global Variable

Do not include the export keyword—this file is parsed by the pam_env module, which does not support shell syntax or commands like export.

After saving and closing the file, log out and back in again. The /etc/environment file is read only at login time (for SSH, terminal, or GUI sessions).

Verify the Global Variable

Output:

Test the Global Variable with a New User

Create a new user account:

Switch to the new user:

Enter the password when prompted, then check whether the global variable is available:

Output:

To delete the global variable, remove its entry from /etc/environment and log out and back in to apply the change.

Use Environment Variables in Bash Scripts

You can reference environment variables—both global and user-defined—inside Bash scripts. The example below demonstrates how to use the GLOBAL_GREETING variable within a simple script.

Create the Script

Add Script Content

#!/bin/bash
echo "${GLOBAL_GREETING}! Welcome to centron!"

Use curly braces {} around the variable name when concatenating it with special characters or text to clearly mark its boundaries. For instance, in the example above, the exclamation mark directly follows the variable name without confusion.

Make the Script Executable

Run the Script

Output:

Hello! Welcome to centron!

You can also use variable nesting within assignments, for example:

This expands $USER when executed. However, such expansions only work for variables defined within a shell configuration file like .bashrc—they won’t function in /etc/environment.

Conclusion

In this guide, you learned how to effectively manage environment variables in Linux. You explored how to view, create, edit, and remove user-specific variables, persist them across sessions, and define global variables for all users. You also discovered how to reference environment variables in Bash scripts. With these techniques, you can better control software behavior and optimize your system’s configuration workflows.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: