Tutorials  /  Linux Basics

Linux Environment Variables: View, Create, Modify & Use in Bash Scripts

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

Environment variables allow you to influence how software behaves without altering the source code. Most Linux distributions include several predefined environment variables. You can also create custom variables to store configuration data, control application behavior, or share values between processes and shell scripts.

This guide shows how to view, create, modify, and delete environment variables in Linux. You will also learn how to make variables persistent across sessions, set global variables for all users, and use them in shell scripts.

Viewing Existing Environment Variables

Linux provides predefined environment variables such as HOME, USER, PWD, and PATH. Follow these steps to check these variables from your terminal session.

List all current environment variables:

Console
$ env

Example 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 use the printenv command to display all environment variables.

Run the following to print your current Linux username:

Console
$ echo $USER

View an environment variable in key-value format using grep:

Console
$ env | grep USER

You can also use:

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

Creating 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 in the current terminal session and disappear after you close the shell.

Define a new environment variable named GREETING:

Console
$ GREETING=Hello

Enclose the value in double quotes if it contains spaces:

Console
$ GREETING="Hello World"

Export the variable so that child processes can access it:

Console
$ export GREETING

Verify that the variable is set:

Console
$ echo $GREETING

Output:

Code
Hello

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

Making Environment Variables Persistent

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

Open your ~/.bashrc file in a text editor:

Console
$ nano ~/.bashrc

Add the following line to the end of the file:

Console
export GREETING=Hello

Save and close the file.

Reload the shell configuration to apply the changes:

Console
$ source ~/.bashrc

Confirm that the environment variable is now available:

Console
$ echo $GREETING

Output:

Code
Hello

Open a new terminal window and check that the variable persists:

Console
$ echo $GREETING

Modifying Environment Variables

You can update environment variables either temporarily or permanently. However, some environment variables are critical to system operations and should only be changed with caution. Always ensure you have the necessary permissions and understand the consequences before making changes. Follow these steps to modify the GREETING environment variable.

Update the GREETING variable value in your current terminal session:

Console
$ export GREETING=Hi

Confirm the new value:

Console
$ echo $GREETING

Output:

Code
Hi

Open your shell configuration file to make the change permanent:

Console
$ nano ~/.bashrc

Modify the value for the GREETING definition:

Console
export GREETING=Hi

Reload the file to apply the changes:

Console
$ source ~/.bashrc

Removing Environment Variables

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

Use unset to remove the GREETING environment variable:

Console
$ unset GREETING

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 (for example ~/.bashrc), it will reappear in new sessions unless you also delete the definition from that file.

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 ideal for defining global values without shell-specific syntax. Follow these steps to create and verify a global environment variable.

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 are readable by all users.

Add your variable without the export keyword:

Code
GLOBAL_GREETING=Hello

Do not include export. The pam_env module, which parses this file, does not support shell syntax or commands like export.

Save and close the file.

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

Run the env command to confirm that the variable is available:

Console
$ env

Output:

Code
GLOBAL_GREETING=Hello

Create a new user:

Console
$ sudo adduser newuser

Switch to the new user:

Console
$ su - newuser

Enter the password when prompted.

Verify that the global environment variable is available to the new user:

Console
$ echo $GLOBAL_GREETING

Output:

Code
Hello

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

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.

Create a new script file named greeting.sh:

Console
$ nano greeting.sh

Add the following script to reference the environment variable:

Code
#!/bin/bash
echo "${GLOBAL_GREETING}! Welcome."

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.

Make the script executable:

Console
$ chmod +x greeting.sh

Run the script:

Console
$ ./greeting.sh

Output:

Code
Hello! Welcome.

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.

Conclusion

In this article, you explored how to manage environment variables in Linux. You viewed, created, modified, and removed user-specific variables, configured them to persist across terminal sessions, and defined global variables accessible by all users. You also learned how to reference these variables in Bash scripts. With this knowledge, you can streamline your workflows and gain better control over how applications behave in different environments.

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?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

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