Master Linux Environment Variables: 6 Configuration Methods & Loading Order Explained
This guide explains how to read, set, and permanently configure Linux environment variables—especially PATH—using six different methods, and reveals the exact order in which the system loads these variable files.
Reading Linux Environment Variables
Use export to list all variables and echo $PATH to display the current PATH value.
uusama@ubuntu:~$ export
declare -x HOME="/home/uusama"
declare -x LANG="en_US.UTF-8"
...
declare -x PATH="/home/uusama/bin:/home/uusama/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
uusama@ubuntu:~$ echo $PATH
/home/uusama/bin:/home/uusama/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binThe PATH variable stores directories separated by colons; you can modify it with or without surrounding quotes.
Six Ways to Configure PATH
Method 1 – Direct export PATH in the current shell
export PATH=/home/uusama/mysql/bin:$PATH # prepend
# or
export PATH=$PATH:/home/uusama/mysql/bin # appendEffective immediately.
Only lasts for the current terminal session.
Applies to the current user.
Remember to include the original $PATH to avoid overwriting existing entries.
Method 2 – Edit ~/.bashrc
Add the line at the end of the file: export PATH=$PATH:/home/uusama/mysql/bin Effective when a new terminal is opened or after running source ~/.bashrc.
Permanent for the user.
Only affects the current user.
If later scripts overwrite PATH, this change may be ignored.
Method 3 – Edit ~/.bash_profile (or ~/.profile )
export PATH=$PATH:/home/uusama/mysql/binEffective after opening a new login shell or running source ~/.bash_profile.
Permanent for the user.
Only affects the current user.
If the file does not exist, edit ~/.profile or create it.
Method 4 – Edit system file /etc/bashrc
# make file writable if needed
chmod u+w /etc/bashrc
vim /etc/bashrc
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective after opening a new terminal or running source /etc/bashrc.
Permanent.
Applies to all users.
Method 5 – Edit system file /etc/profile
# make file writable if needed
chmod u+w /etc/profile
vim /etc/profile
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective after opening a new terminal or running source /etc/profile.
Permanent.
Applies to all users.
Method 6 – Edit /etc/environment
# make file writable if needed
chmod u+w /etc/environment
vim /etc/environment
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective after opening a new terminal or running source /etc/environment.
Permanent.
Applies to all users.
How Linux Loads Environment Variables
Variables are read from both user‑level files and system‑level files. User‑level files include ~/.bashrc, ~/.profile (or ~/.bash_profile). System‑level files include /etc/bashrc, /etc/profile, and /etc/environment.
During a login, the shell reads ~/.bash_profile (or ~/.profile); if absent it reads ~/.bash_login. Those files may source ~/.bashrc, which is read for each interactive shell.
Testing the Loading Order
By inserting a line that appends the file name to a custom variable UU_ORDER in each configuration file, then opening a new terminal and running echo $UU_ORDER, the observed order is:
/etc/environment:/etc/profile:/etc/bash.bashrc:/etc/profile.d/test.sh:~/.profile:~/.bashrc/etc/environment
/etc/profile
/etc/bash.bashrc
/etc/profile.d/test.sh (if present)
~/.profile (or ~/.bash_profile)
~/.bashrc
File Loading Details
/etc/profileloads /etc/bash.bashrc and then iterates over /etc/profile.d/*.sh. The ~/.profile file, when executed, sources ~/.bashrc. The ~/.profile is read only once at login, while ~/.bashrc is read for each new shell.
Additional Tips
Create a custom file (e.g., uusama.profile) with many export statements and source it from ~/.profile to reuse variables across sessions.
Define command aliases, such as alias rm="rm -i", and add them to ~/.profile for safer usage.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
