Tutorials  /  Linux Basics

Manage Environment Variables in Linux: Full Guide

Ccentron Redaktion · October 2025 ·9 min read ·Linux Basics, Tutorial

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

Console
$ env

Output:

Code
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.

Console
$ echo $USER

Search for a Specific Variable

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

Console
$ env | grep USER

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

VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

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

Console
$ GREETING=Hello

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

Console
$ GREETING="Hello World"

Export the Variable

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

Console
$ export GREETING

Verify the Variable

Console
$ echo $GREETING

Output:

Code
Hello

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

Console
$ nano ~/.bashrc

Add the Variable Definition

Console
$ export GREETING=Hello

Save the file and close the editor.

Apply the Changes

Console
$ source ~/.bashrc

Verify the Persistent Variable

Console
$ echo $GREETING

Output:

Code
Hello

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

Console
$ echo $GREETING

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:

Console
$ export GREETING=Hi

Check the new value:

Console
$ echo $GREETING

Output:

Code
Hi

Persist the Updated Variable

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

Console
$ nano ~/.bashrc

Update the variable definition as follows:

Console
$ export GREETING=Hi

Reload the file to apply your changes:

Console
$ source ~/.bashrc

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

Console
$ unset GREETING

Verify Removal

Console
$ echo $GREETING

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

Console
$ 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

Console
$ GLOBAL_GREETING=Hello

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

Console
$ env

Output:

Code
GLOBAL_GREETING=Hello

Test the Global Variable with a New User

Create a new user account:

Console
$ sudo adduser newuser

Switch to the new user:

Console
$ su - newuser

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

Console
$ echo $GLOBAL_GREETING

Output:

Code
Hello

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

Console
$ nano greeting.sh

Add Script Content

Code
#!/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

Console
$ chmod +x greeting.sh

Run the Script

Console
$ ./greeting.sh

Output:

Code
Hello! Welcome to centron!

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

Code
GREETING="Hi! $USER"

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.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren