Master Linux Environment Variable Configuration: Six Methods and Loading Order Explained
This guide explains how to configure Linux environment variables—especially PATH for MySQL—using six different methods, details each method's scope and persistence, and reveals the exact order in which the system loads user and system configuration files.
Linux Environment Variable Configuration
When installing custom software on Linux, you often need to add directories to the PATH or set other environment variables. The example below assumes an Ubuntu 14 system, user uusama, and a MySQL binary directory at /home/uusama/mysql/bin.
Reading Environment Variables
export– displays all currently defined environment variables. echo $PATH – prints the current value of PATH.
uusama@ubuntu:~$ export
declare -x HOME="/home/uusama"
declare -x LANG="en_US.UTF-8"
... (other variables) ...
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 defines where the shell searches for executable files; entries are separated by colons ( :). You can modify it with or without surrounding quotes.
Method 1: Direct export PATH Command
Append the MySQL directory to PATH directly in the current shell:
export PATH=/home/uusama/mysql/bin:$PATH # prepend
# or
export PATH=$PATH:/home/uusama/mysql/bin # appendEffective immediately.
Only affects the current terminal session.
Limited to the current user.
Remember to include the existing $PATH to avoid overwriting previous 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 for new terminals or after running source ~/.bashrc.
Permanent for the user.
May be overridden if later files modify PATH.
Method 3: Edit ~/.bash_profile (or ~/.profile )
Similar to ~/.bashrc, but placed in the login‑profile file:
vim ~/.bash_profile
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective after a new login session or after source ~/.bash_profile.
Permanent for the user.
If ~/.bash_profile does not exist, edit ~/.profile or create the file.
Method 4: Edit System File /etc/bashrc
Requires root privileges. Make the file writable, then append the export line:
# make writable if needed
chmod u+w /etc/bashrc
vim /etc/bashrc
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective for new terminals or after source /etc/bashrc.
Permanent for all users.
Method 5: Edit System File /etc/profile
Also requires root. The steps mirror Method 4:
# make writable if needed
chmod u+w /etc/profile
vim /etc/profile
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective for new terminals or after source /etc/profile.
Permanent for all users.
Method 6: Edit System File /etc/environment
This file is read by the PAM module at login. Add the export line similarly:
# make writable if needed
chmod u+w /etc/environment
vim /etc/environment
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective after a new login or after source /etc/environment.
Permanent for all users.
How Linux Loads Environment Variable Files
Environment variables are divided into user‑level (e.g., ~/.bashrc, ~/.profile) and system‑level (e.g., /etc/bashrc, /etc/profile, /etc/environment) files. During a login, the shell reads files in a specific order, and later definitions can overwrite earlier ones.
Testing the Loading Order
To observe the order, add the same variable UU_ORDER to the first line of each file, appending the file name to its value: export UU_ORDER="$UU_ORDER:/path/to/file" After saving and opening a new terminal, run echo $UU_ORDER to see the concatenated list. The observed order is:
/etc/environment
/etc/profile
/etc/bash.bashrc
/etc/profile.d/test.sh (if present)
~/.profile (or ~/.bash_profile)
~/.bashrc
Detailed File Loading
/etc/profileloads /etc/bash.bashrc and then iterates over /etc/profile.d/*.sh. The user’s ~/.profile (or ~/.bash_profile) subsequently sources ~/.bashrc. The ~/.profile is read once at login, while ~/.bashrc is read for each interactive shell.
# Example snippet from /etc/profile
if [ "PS1" ]; then
if [ "BASH" ] && [ "BASH" != "/bin/sh" ]; then
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1=' '
fi
fi
fi
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r i ]; then
. i
fi
done
unset i
fi # Example snippet from ~/.profile
if [ -n "BASH_VERSION" ]; then
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
PATH="$HOME/bin:$HOME/.local/bin:$PATH"Additional Tips
Create a custom file (e.g., uusama.profile) with your own export statements and source it from ~/.profile to keep project‑specific variables organized.
Define command aliases (e.g., alias rm="rm -i") in ~/.profile or ~/.bashrc 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.
