Master Linux Environment Variables: 6 Proven Configuration Methods and Loading Order
This guide explains how to configure Linux environment variables—including PATH for MySQL—using six different methods, demonstrates how to read them, details the system's loading sequence, and provides practical tips and test procedures for reliable setup.
When installing custom software on Linux, configuring environment variables is essential; the article uses an Ubuntu 14.0 system with user uusama and a MySQL binary directory /home/uusama/mysql/bin as the example.
Reading Environment Variables
The commands export (to list all variables) and echo $PATH (to display the current PATH) are shown with sample output, illustrating that PATH is a colon‑separated list of directories.
Method 1: Direct export PATH
export PATH=/home/uusama/mysql/bin:$PATH
# or
export PATH=$PATH:/home/uusama/mysql/binEffective immediately.
Only the current terminal session is affected; closing the window discards the change.
Applies solely to the current user.
Remember to append the existing $PATH to avoid overwriting previous entries.
Method 2: Edit ~/.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 )
vim ~/.bash_profile
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective when a new terminal is opened or after running source ~/.bash_profile.
Permanent for the user.
Only the current user is affected.
If ~/.bash_profile does not exist, edit ~/.profile or create the file.
Method 4: Edit /etc/bashrc (system‑wide)
# Make the file writable if necessary
chmod -v 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.
Applies to all users.
Method 5: Edit /etc/profile (system‑wide)
# Make the file writable if necessary
chmod -v 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.
Applies to all users.
Method 6: Edit /etc/environment (system‑wide)
# Make the file writable if necessary
chmod -v u+w /etc/environment
vim /etc/environment
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective for new terminals or after source /etc/environment.
Permanent.
Applies to all users.
Loading Principle and File Classification
Environment variables are divided into user‑level (e.g., ~/.bashrc, ~/.profile / ~/.bash_profile) and system‑level files (e.g., /etc/bashrc, /etc/profile, /etc/environment). When a user logs in, the shell reads ~/.bash_profile (or ~/.profile), then ~/.bashrc. System files are processed earlier.
Testing the Loading Order
To verify the order, the same variable UU_ORDER is exported in the first line of each file, appending the file’s name. After reopening a terminal, echo $UU_ORDER yields:
$UU_ORDER:/etc/environment:/etc/profile:/etc/bash.bashrc:/etc/profile.d/test.sh:~/.profile:~/.bashrcFrom this result the loading sequence is inferred as:
/etc/environment
/etc/profile
/etc/bash.bashrc
/etc/profile.d/test.sh
~/.profile (or ~/.bash_profile)
~/.bashrc
Detailed File Loading
The /etc/profile script loads /etc/bash.bashrc and then iterates over /etc/profile.d/*.sh. Sample excerpt:
# /etc/profile: system-wide .profile file for the Bourne shell (sh)
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
fiThe ~/.profile script includes ~/.bashrc if it exists and sets a user‑specific PATH.
# if running bash
if [ -n "BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "HOME/.bashrc" ]; then
. "HOME/.bashrc"
fi
fi
# set PATH so it includes user's private bin directories
PATH="HOME/bin:HOME/.local/bin:PATH"Additional Tips
Create a custom profile file (e.g., uusama.profile) with your own export statements and source it from ~/.profile for project‑specific variables.
Define command aliases such as alias rm="rm -i" in ~/.profile to make destructive commands safer.
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.
