
Environment variables let you control how software behaves without modifying the underlying code. Most Linux distributions include a set of predefined environment variables. You can also define custom variables to store config data, control application behavior, or share values between processes and shell scripts.
This article explains how to view, create, modify, and delete environment variables in Linux. You'll learn how to make variables persistent across sessions, define global variables for all users, and use them inside shell scripts.
Linux systems come with several predefined environment variables such as HOME, USER, PWD, and PATH. Use the following steps to inspect these variables from your terminal session.
List all current environment variables.
Output:
You can also use the printenv command to display all environment variables.
Run echo $USER to print your current Linux username.
View an environment variable in key-value format using grep.
You can also use printenv | grep USER to achieve the same result.
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.
Define a new environment variable named GREETING.
Enclose the value in double quotes if it includes spaces, for example:
Export the variable so that child processes can access it.
Verify that the variable is set.
Output:
Temporary environment variables only last for the duration of the current shell session. Once you close the terminal, they will be removed automatically.
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 is loaded every time you start a new session.
Open your ~/.bashrc file in a text editor.
Add the following line to the bottom of the file.
Save and close the file.
Reload the shell configuration to apply the changes.
Confirm that the environment variable is now available.
Output:
Open a new terminal window and verify the variable persists.
You can update environment variables either temporarily or permanently. However, 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. Follow the steps below to modify the GREETING environment variable.
Update the GREETING variable value in your current terminal session.
Confirm the new value.
Output:
Open your shell configuration file to persist the change across sessions.
Modify the value for the GREETING definition.
Reload the file to apply the changes.
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.
Use unset to remove the GREETING environment variable.
Verify that the variable has been removed.
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.
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. Follow the steps below to create and verify a global environment variable.
Open /etc/environment file using Nano.
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.
Add your variable without the export keyword:
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.
Output:
Create a new user.
Switch to the new user.
Enter the password when prompted.
Verify that the global environment variable is available to the new user.
Output:
To remove the variable, delete its entry from /etc/environment and log out and back in again.
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.
Add the following script to reference the environment variable.
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.
Run the script.
Output:
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.
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.
For further reading, refer to:
0 Comments
Be the first to comment and share your perspective with the community.