In Linux, an environment variable is a value that can be used to provide configuration and data across different parts of a system.
These variables act as placeholders for data, allowing applications and the shell to interact with each other more smoothly.
One primary advantage of environment variables is that they facilitate the sharing of values between processes, enabling centralized control over aspects of system behavior.
- 1 Scope (Local vs Global)
- 2 Listing Environment Variables
- 3 Setting Environment Variables (Permanent variables)
- 4 Setting Environment Variables (System-wide)
- 5 /etc/environment vs. bashrc
- 6 Unsetting Environment Variables
- 7 Special Environment Variables
- 8 Using Environment Variables in Bash Scripting
- 9 Systemd and Environment Variables
- 10 Loading Environment Variables from Files
- 11 F.A.Q
Scope (Local vs Global)
Environment variables in Linux can be classified into two main categories based on their scope: Local and Global.
Local variables are limited to the current shell session. Any sub-shell or program initiated within that session can access these variables, but they are not available in other sessions.
Here’s how you can create a local variable:
variable_name="local value" echo $variable_name
In this code, you are declaring a variable named
variable_name and assigning it the value “local value.” When using the
echo command to print the variable, it returns the assigned value.
This variable is available only within the current shell and will not be recognized in new terminal windows or other sessions.
Global variables, also known as environment variables, are accessible to all sessions and users on the system.
They provide a way to share configuration settings between different applications and shell sessions.
You can create a global variable using the
export GLOBAL_VARIABLE="global value" echo $GLOBAL_VARIABLE
By prefixing the declaration with
export, you are making the variable
GLOBAL_VARIABLE accessible to all sessions and users on the system.
Unlike local variables, global variables retain their value across different terminal sessions.
Listing Environment Variables
In Linux, you have various methods to list environment variables. Here, we will explore different ways to do that.
printenv command can list all environment variables or display the value of a specific variable.
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOME=/home/username ...
By simply running
printenv, you get a list of all environment variables. If you want to view a specific variable, such as
The above command specifically displays the
env command can also list all the environment variables.
SHELL=/bin/bash USER=username ...
It returns a comprehensive list of all the environment variables active on the system.
To print the value of a specific environment variable, you can use the
echo command with the variable name prefixed by
This returns the value of the
USER variable, showing the current logged-in username.
In some Linux distributions, the
/etc/environment file holds global environment variables. You can view its contents with a text editor or the
This will display the contents of the file, including any defined global environment variables.
Setting Environment Variables (Permanent variables)
When you want to make environment variables permanently available across all sessions and after reboots, you’ll need to add them to one of the shell configuration files.
.bashrc file is a script that runs every time you open a new terminal window. You can add your environment variables here.
.bashrc with a text editor like
Add your export line at the end of the file:
Save and exit the file.
Now, apply the changes with:
Or you can restart the terminal.
NEW_VARIABLE is now permanently set and won’t disappear after reboot.
Editing .bash_profile or .profile
Some systems use
.profile for login shells. The process is the same as with
.profile in the above instructions.
The choice between these files depends on your system and personal preferences.
.bashrc is used for non-login shells, and
.profile for login shells. Environment variables can be placed in any of them, depending on your specific needs.
By adding environment variables to these configuration files, you ensure they are globally accessible across different terminal sessions and after system restarts.
Setting Environment Variables (System-wide)
System-wide environment variables are accessible to all users on the system. These variables can be set in specific files that are read during the system boot or user login.
/etc/environment file is a simple text file containing key-value pairs. You can add an environment variable here:
sudo echo 'GLOBAL_PATH="/new/path"' | sudo tee -a /etc/environment
Then, you’ll need to log out and log back in, or restart the system.
GLOBAL_PATH variable has now been set for all users on the system.
Alternatively, you can use the
/etc/profile file to set system-wide environment variables. Open the file:
sudo nano /etc/profile
Add your export line:
export SYSTEM_VARIABLE="system value"
Save and exit.
To apply the changes, either restart the system or run:
SYSTEM_VARIABLE is now set globally for all users.
Both methods allow you to set environment variables that are accessible by all users, ensuring a consistent environment across the system.
/etc/environment vs. bashrc
|Scope||System-wide (all users)||User-specific|
|Type of sessions||All types (including GUI like KDE and GNOME)||Interactive non-login shells|
||Shell scripting syntax|
|Effect||Variables loaded upon login||Variables loaded on new shell|
|Suitable for||Setting environment variables for all users and sessions||Setting user-specific aliases, functions, and shell variables|
|Impact on GUI applications||Variables available||Variables are not be available|
|Requires shell restart||No (changes apply at next login)||Yes (for changes to take effect in current session)|
Unsetting Environment Variables
There may be times when you want to remove an environment variable, either for testing purposes or to clear an unnecessary or incorrect setting.
unset command allows you to do just that.
Here’s how to unset an environment variable:
export VARIABLE_TO_UNSET="value" echo $VARIABLE_TO_UNSET
The above code first sets a variable named
VARIABLE_TO_UNSET with the value “value.” Now, you’ll unset it:
unset VARIABLE_TO_UNSET echo $VARIABLE_TO_UNSET
The variable has been removed, so attempting to
echo it returns a blank line.
If the variable was set in one of the shell configuration files or system-wide files, you would need to remove the corresponding line from the file and reload the file with
source or restart the session.
Special Environment Variables
Linux defines several special environment variables that control various aspects of the system’s behavior. Understanding these can help you in system configuration and scripting.
PATH variable defines the directories where the system looks for executable files.
This shows the directories that the system searches for executables, allowing you to run commands without specifying their full paths.
HOME variable points to the current user’s home directory.
This provides the path to the user’s home directory, useful in scripts or commands that need to reference user-specific files or directories.
This variable contains the current logged-in username.
SHELL variable defines the default shell for the user.
This variable contains the system’s hostname.
LANG variable sets the system’s language and locale.
(Prompt String 1): This is the environment variable that defines the primary prompt string. It is the string of characters that are displayed when the terminal is waiting for input.
(Prompt String 2): This is the secondary prompt that is displayed when a command needs more input to complete.
For example, if you type
echo " without closing the quote and hit Enter, the secondary prompt (
> by default) will appear on the next line.
This is to show you that the command is not yet complete, and it’s waiting for the closing quote.
PWD variable contains the current working directory.
PWD environment variable is not stored in a physical file but in the system’s memory when a shell session is created.
HISTSIZE variable defines the size of the command history.
These variables can be set to define the default text editor and web browser.
Using Environment Variables in Bash Scripting
Here’s a basic example of how you can use environment variables in a bash script:
Create a file called
#!/bin/bash MY_NAME="John" export MY_OS="Linux" echo "Hello, $MY_NAME. Welcome to $MY_OS!"
Now, make the script executable:
chmod +x example.sh
Run the script:
Hello, John. Welcome to Linux!
MY_OS are environment variables set within the script.
export command makes
MY_OS available to any subprocesses. They’re then used in an echo statement to print a welcome message.
You can even pass environment variables from the command line when running a script. This can be useful to change the script’s behavior based on input.
Hello, Alice. Welcome to Linux!
By prefixing the script run with
MY_NAME="Alice", you’ve temporarily overridden the
MY_NAME variable in the script, allowing for customizable output.
Utilizing environment variables in scripting allows you to write dynamic, reusable code.
Systemd and Environment Variables
systemd is an initialization system used to bootstrap the user space and manage all subsequent processes.
To set environment variables for a specific service using
systemd, you will typically edit the service’s unit file.
Here’s an example:
sudo systemctl edit my-service.service
[Service] Environment="MY_VARIABLE=my value"
systemd and restart the service:
sudo systemctl daemon-reload sudo systemctl restart my-service.service
MY_VARIABLE is now available to the
my-service service, with the value “my value.”
You can also use a dedicated environment file:
Create a file
/etc/my-service.env with the content:
Refer to this file in the service’s unit file:
Reload and restart as before.
This allows for a clean separation of configuration, which can be particularly useful with complex services.
Loading Environment Variables from Files
Here are two common ways to achieve that:
source command reads and executes commands from the given filename in the current shell environment.
Create a file called
variables.sh with the following content:
export VAR1="value1" export VAR2="value2"
To load these variables, run:
echo $VAR1 echo $VAR2
The variables are now available in your current shell.
dotenv is a tool that can load variables from a
.env file into the environment. You’ll need to install it first.
Create a file called
.env with the following content:
Load these variables using
Verify in the same way as before.
Q: How to use the
export command to set the default editor?
A: To set the default editor using the
export command, you need to modify the
EDITOR environment variable. For example, if you want to set
vim as your default editor, you can use the following command:
If you want to make it permanent, add the above line to your shell configuration file (
~/.profile), then save and close the file. To make the changes take effect, you can use the
Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.