Set Environment Variables in Linux Permanent and System-wide
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
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
Output:
local value
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
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
command:
export GLOBAL_VARIABLE="global value" echo $GLOBAL_VARIABLE
Output:
global value
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.
Using printenv
The printenv
command can list all environment variables or display the value of a specific variable.
printenv
Output:
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 HOME
, use:
printenv HOME
Output:
/home/username
The above command specifically displays the HOME
variable.
Using env
Similar to printenv
, the env
command can also list all the environment variables.
env
Output:
SHELL=/bin/bash USER=username ...
It returns a comprehensive list of all the environment variables active on the system.
Using echo
To print the value of a specific environment variable, you can use the echo
command with the variable name prefixed by $
.
echo $USER
Output:
username
This returns the value of the USER
variable, showing the current logged-in username.
Using /etc/environment
In some Linux distributions, the /etc/environment
file holds global environment variables. You can view its contents with a text editor or the cat
command.
cat /etc/environment
Output:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
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.
Editing .bashrc
The .bashrc
file is a script that runs every time you open a new terminal window. You can add your environment variables here.
Open .bashrc
with a text editor like nano
:
nano ~/.bashrc
Add your export line at the end of the file:
export NEW_VARIABLE="value"
Save and exit the file.
Now, apply the changes with:
source ~/.bashrc
Or you can restart the terminal.
Verify with:
echo $NEW_VARIABLE
Output:
value
The variable NEW_VARIABLE
is now permanently set and won’t disappear after reboot.
Editing .bash_profile or .profile
Some systems use .bash_profile
or .profile
for login shells. The process is the same as with .bashrc
.
Just replace .bashrc
with .bash_profile
or .profile
in the above instructions.
The choice between these files depends on your system and personal preferences.
Generally, .bashrc
is used for non-login shells, and .bash_profile
or .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
The /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.
Verify with:
printenv GLOBAL_PATH
Output:
/new/path
The GLOBAL_PATH
variable has now been set for all users on the system.
/etc/profile
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:
source /etc/profile
Verify with:
echo $SYSTEM_VARIABLE
Output:
system value
The 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
Aspect | /etc/environment | ~/.bashrc |
---|---|---|
Scope | System-wide (all users) | User-specific |
Type of sessions | All types (including GUI like KDE and GNOME) | Interactive non-login shells |
Syntax | key=value pairs only |
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) |
Accepts scripting | No | Yes |
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.
The 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
Output:
value
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
Output:
(blank line)
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
The PATH
variable defines the directories where the system looks for executable files.
echo $PATH
Output:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
This shows the directories that the system searches for executables, allowing you to run commands without specifying their full paths.
HOME
The HOME
variable points to the current user’s home directory.
echo $HOME
Output:
/home/username
This provides the path to the user’s home directory, useful in scripts or commands that need to reference user-specific files or directories.
USER
This variable contains the current logged-in username.
echo $USER
Output:
username
SHELL
The SHELL
variable defines the default shell for the user.
echo $SHELL
Output:
/bin/bash
HOSTNAME
This variable contains the system’s hostname.
echo $HOSTNAME
Output:
your-hostname
LANG
The LANG
variable sets the system’s language and locale.
echo $LANG
Output:
en_US.UTF-8
PS1
(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.
echo $PS1
Output:
[\u@\h \W]\$
PS2
(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.
echo $PS2
Output:
>
PWD
The PWD
variable contains the current working directory.
echo $PWD
Output:
/home/username/directory
The PWD
environment variable is not stored in a physical file but in the system’s memory when a shell session is created.
HISTSIZE
The HISTSIZE
variable defines the size of the command history.
echo $HISTSIZE
Output:
1000
EDITOR
, BROWSER
These variables can be set to define the default text editor and web browser.
echo $EDITOR
Output:
/usr/bin/vim
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 example.sh
:
#!/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:
./example.sh
Output:
Hello, John. Welcome to Linux!
Here, MY_NAME
and MY_OS
are environment variables set within the script.
The 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.
MY_NAME="Alice" ./example.sh
Output:
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
Add an [Environment]
section:
[Service] Environment="MY_VARIABLE=my value"
Reload systemd
and restart the service:
sudo systemctl daemon-reload sudo systemctl restart my-service.service
The variable 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:
MY_VARIABLE=my value
Refer to this file in the service’s unit file:
[Service] EnvironmentFile=/etc/my-service.env
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:
Using source
The 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:
source variables.sh
Verify with:
echo $VAR1 echo $VAR2
Output:
value1 value2
The variables are now available in your current shell.
Using dotenv
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:
VAR1=value1 VAR2=value2
Load these variables using dotenv
:
dotenv
Verify in the same way as before.
Output:
value1 value2
F.A.Q
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:
export EDITOR=vim
If you want to make it permanent, add the above line to your shell configuration file (~/.bashrc
, ~/.bash_profile
, or ~/.profile
), then save and close the file. To make the changes take effect, you can use the source
command:
source ~/.bashrc
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.