Master Linux Environment Variables: 6 Configuration Methods & Loading Order Explained
This guide walks through six practical ways to set Linux environment variables—including export commands, editing ~/.bashrc, ~/.bash_profile, /etc/bashrc, /etc/profile, and /etc/environment—explains how the system loads them, and provides testing tips and useful shell tricks.
Linux Environment Variable Configuration
When installing custom software on Linux you often need to add directories to the PATH or define other variables. The examples below assume Ubuntu 14.0, user uusama, and a MySQL binary directory /home/uusama/mysql/bin.
Reading Environment Variables
export– lists all exported variables. echo $PATH – prints 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 is a colon‑separated list of directories that the shell searches for executables. It can be modified with or without surrounding quotes.
Method 1: export PATH
Directly prepend or append the new directory:
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 keep the original $PATH to avoid overwriting existing entries.
Method 2: Edit ~/.bashrc
Add the export line at the end of the file:
vim ~/.bashrc
# at the last line
export PATH=$PATH:/home/uusama/mysql/binEffective when a new terminal is opened or after running source ~/.bashrc.
Permanent for the user.
May be overridden by later configuration files.
Method 3: Edit ~/.bash_profile (or ~/.profile )
Same as the previous method, but placed in the login‑shell file:
vim ~/.bash_profile
# at the last line
export PATH=$PATH:/home/uusama/mysql/binEffective after a new login session or after source ~/.bash_profile.
Permanent for the user.
If the file does not exist, edit ~/.profile or create it.
Method 4: Edit /etc/bashrc (system‑wide)
Requires root privileges:
chmod -v u+w /etc/bashrc
vim /etc/bashrc
# at the last line
export PATH=$PATH:/home/uusama/mysql/binEffective for new terminals or after source /etc/bashrc.
Permanent for all users.
Method 5: Edit /etc/profile (system‑wide)
Also requires root:
chmod -v u+w /etc/profile
vim /etc/profile
# at the last line
export PATH=$PATH:/home/uusama/mysql/binEffective for new login sessions or after source /etc/profile.
Permanent for all users.
Method 6: Edit /etc/environment
System‑wide environment file (root required):
chmod -v u+w /etc/environment
vim /etc/environment
# add or modify the line
PATH="$PATH:/home/uusama/mysql/bin"Effective for new sessions or after source /etc/environment.
Permanent for all users.
How Linux Loads Environment Variables
Variables are read from both user‑level and system‑level files. The typical loading order is:
/etc/environment
/etc/profile
/etc/bash.bashrc (or /etc/bashrc)
/etc/profile.d/*.sh
~/.profile (or ~/.bash_profile)
~/.bashrc
User‑level files are processed after system‑level ones, and ~/.bashrc is sourced on every interactive shell, while ~/.profile is read only at login.
Testing the Loading Order
To verify the order, add the same variable UU_ORDER to the first line of each file, appending the file name: export UU_ORDER="$UU_ORDER:/path/to/file" After opening a new terminal, echo $UU_ORDER will show the concatenated list, confirming the sequence shown above.
Additional Tips
Create a project‑specific file (e.g., uusama.profile) and source it from ~/.profile to load custom variables automatically.
Define command aliases, such as alias rm="rm -i", in ~/.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.
