Master Linux Environment Variables: Multiple Configuration Methods & Loading Order
This guide explains how to configure Linux environment variables using various methods such as export PATH, editing ~/.bashrc, ~/.bash_profile, /etc/bashrc, /etc/profile, and /etc/environment, and details the exact order in which these files are loaded by the system.
Linux Environment Variable Configuration
When installing software manually, you often need to set environment variables. The examples below assume Ubuntu 14.0, user uusama, and the MySQL binary directory /home/uusama/mysql/bin.
Reading Environment Variables
Use export to list all defined variables.
Use 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 LANGUAGE="en_US:"
declare -x PATH="/home/uusama/bin:/home/uusama/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"The PATH variable contains a colon‑separated list of directories that the shell searches for executable files.
Method 1: export PATH
Directly modify PATH with the export command:
export PATH=/home/uusama/mysql/bin:$PATH
# or place the new path at the end
export PATH=$PATH:/home/uusama/mysql/binEffective immediately.
Only affects 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 export line to the end of ~/.bashrc:
vim ~/.bashrc
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective when a new terminal is opened or after running source ~/.bashrc.
Permanent for the user.
Only the current user is affected.
If later files overwrite PATH, the change may not take effect.
Method 3: Edit ~/.bash_profile (or ~/.profile )
Similar to ~/.bashrc, add the export line at the end:
vim ~/.bash_profile
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective when a new login shell starts or after source ~/.bash_profile.
Permanent for the user.
If ~/.bash_profile does not exist, edit ~/.profile or create a new file.
Method 4: Edit /etc/bashrc (system‑wide)
Requires root privileges:
chmod -v u+w /etc/bashrc
vim /etc/bashrc
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective for all users after opening a new terminal or running source /etc/bashrc.
Permanent.
Applies system‑wide.
Method 5: Edit /etc/profile (system‑wide)
Also requires root privileges:
chmod -v u+w /etc/profile
vim /etc/profile
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective for all users after a new login or source /etc/profile.
Permanent.
Applies system‑wide.
Method 6: Edit /etc/environment (system‑wide)
Requires root privileges:
chmod -v u+w /etc/environment
vim /etc/environment
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective after a new login or source /etc/environment.
Permanent.
Applies system‑wide.
How Linux Loads Environment Variables
Environment variables are loaded in a specific order, which determines which definitions take precedence.
User‑level files: ~/.bashrc, ~/.profile (or ~/.bash_profile).
System‑level files: /etc/bashrc, /etc/profile (or /etc/bash_profile), /etc/environment.
The system first reads /etc/environment, then /etc/profile, which may source /etc/bash.bashrc and scripts in /etc/profile.d/. After that, the user’s ~/.profile is read, which in turn may source ~/.bashrc. The .bashrc file is read for each interactive shell.
Testing the Loading Order
To verify the order, add the following line as the first line of each file listed below, replacing the placeholder with the file’s absolute path: export UU_ORDER="$UU_ORDER:/path/to/file" Files to modify:
/etc/environment
/etc/profile
/etc/profile.d/test.sh (optional)
/etc/bashrc or /etc/bash.bashrc
~/.bash_profile or ~/.profile
~/.bashrc
After editing, open a new terminal and run echo $UU_ORDER. The output reveals the loading sequence, which is:
/etc/environment
/etc/profile
/etc/bash.bashrc
/etc/profile.d/test.sh
~/.profile
~/.bashrc
Additional Tips
Create a custom file (e.g., uusama.profile) with a series of export statements, then source it from ~/.profile to load project‑specific variables on login.
Define command aliases with alias, such as alias rm="rm -i", and place them in ~/.profile for convenient use.
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.
