Environment variables store temporary data like paths, configuration settings, and tokens. Programs and scripts access these values without hardcoding them into files. This guide shows how to set an environment variable in Linux for current sessions and across system restarts.
What Are Environment Variables in Linux
Environment variables store system and user configuration data. Each user has a unique set of variables that define their working environment. An admin account differs from a standard user account in terms of available variables and permissions.
Variables can be user-specific or system-wide. Examples include personal API keys visible only to one user or global paths accessible to all users.
Common environment variables:
| Variable | Description |
|---|---|
USER |
Current logged-in username |
HOME |
User home directory path |
SHELL |
Active shell path like bash or zsh |
LANG |
System language and locale settings |
MAIL |
User mailbox location |
PATH |
Directories searched for executables |
These variables exist only for the current session unless configured for persistence.
How to List Environment Variables in Linux
Display all environment variables for your session:
$ env
Output shows all defined variables:
SHELL=/bin/bash
PWD=/home/user
USER=user
HOME=/home/user
LANG=C.UTF-8
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MAIL=/var/mail/user
How to Print Environment Variables in Linux
Two methods display specific variable values.
Using printenv Command
Print a single variable:
$ printenv SHELL
Returns:
/bin/bash
Using echo Command
Print using the dollar sign prefix:
$ echo $SHELL
Returns:
/bin/bash
Both methods produce identical output. The echo command requires the dollar sign before the variable name.
How to Set Environment Variables in Linux
Use the export command to define variables:
$ export JAVA_HOME=/usr/bin/java
Verify the variable exists:
$ echo $JAVA_HOME
Returns:
/usr/bin/java
Variables set this way exist only in the current session. Close the terminal and the variable disappears.
How to Make Environment Variables Persistent in Linux
Store variables in configuration files to preserve them across sessions.
Edit .bashrc for User Variables
Open the .bashrc file in your home directory:
$ vi ~/.bashrc
Add the export statement at the end:
export JAVA_HOME=/usr/bin/java
Apply changes without logging out:
$ source ~/.bashrc
Test by opening a new terminal:
$ echo $JAVA_HOME
The variable persists across all future sessions for your user account.
How to Create a Global Environment Variable in Linux
Global variables are accessible by all users on the system. Define them in system-wide configuration files.
Using /etc/environment
Edit the file with root privileges:
# vi /etc/environment
Add the variable without using export:
GLOBAL_VARIABLE="This is a global variable"
Apply changes:
# source /etc/environment
Using /etc/profile
Edit the profile file:
# vi /etc/profile
Add the variable with export:
export GLOBAL_VARIABLE="This is a global variable"
Test from any user account:
$ echo $GLOBAL_VARIABLE
Returns:
This is a global variable
All users can now access this variable regardless of their session.
How to Verify Environment Variables Work
Check if a variable persists across sessions:
Open a new terminal window.
Print the variable:
$ echo $JAVA_HOME
If the output shows the value, the variable is persistent. Empty output means the variable was not saved correctly or the configuration file was not sourced.
Verify global variables by switching users:
$ su - username
$ echo $GLOBAL_VARIABLE
The variable should display for all user accounts.
Frequently Asked Questions
Add the export command to your .bashrc file in your home directory, then run source ~/.bashrc to apply changes immediately.
.bashrc runs for interactive non-login shells, while .bash_profile runs only for login shells. Use .bashrc for most environment variables.
Run the env command in your terminal to display all environment variables and their current values for your session.
Yes, edit /etc/environment or /etc/profile with root privileges to create system-wide variables accessible to all users on the system.
Use the unset command followed by the variable name: unset VARIABLE_NAME. This removes it from the current session only.

