Master Linux Environment Variables: 6 Proven Configuration Methods
This guide explains how to read, configure, and persist Linux environment variables—especially PATH—using export commands, user‑level files like ~/.bashrc and ~/.profile, and system‑wide files such as /etc/profile, /etc/bashrc, and /etc/environment, while also detailing the loading order and testing techniques.
Linux Environment Variable Configuration
When installing software on Linux, you often need to add its binaries to the environment so the system can locate them. The examples below assume Ubuntu 14.0, user uusama, and a MySQL binary directory /home/uusama/mysql/bin.
System: Ubuntu 14.0
Username: uusama
MySQL path to add: /home/uusama/mysql/bin
Reading Environment Variables
export– displays all currently defined environment variables. echo $PATH – prints the value of the PATH variable.
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 lists directories separated by colons; export can be used with or without double quotes.
Method 1: export PATH
Directly modify PATH with the export command.
export PATH=/home/uusama/mysql/bin:$PATH
# or put the new directory first
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
Append an export line to the user’s ~/.bashrc file.
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.
May be overridden by later configuration files.
Method 3: Edit ~/.bash_profile
Similar to ~/.bashrc, but used by login shells.
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 instead.
Method 4: Edit /etc/bashrc
System‑wide configuration; requires root privileges.
# make the file writable
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
Another system‑wide file, similar to /etc/bashrc.
# make the file writable
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 environment file; also requires root.
# make the file writable
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.
Linux Environment Variable Loading Principle
Environment variables can be defined at the user level or system level. The system reads files in a specific order, so later definitions can overwrite earlier ones.
User‑level files: ~/.bashrc, ~/.profile (or ~/.bash_profile).
System‑level files: /etc/bashrc, /etc/profile (or /etc/bash_profile), /etc/environment.
During a login, the shell first reads ~/.bash_profile (or ~/.profile), then ~/.bashrc. System files are processed before user files.
Testing the Loading Order
Insert the same line into each file to trace the order:
export UU_ORDER="$UU_ORDER:/path/to/file"After opening a new shell, run echo $UU_ORDER to see the concatenated list.
$UU_ORDER:/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
~/.profile
~/.bashrc
File Loading Details
/etc/profileloads /etc/bash.bashrc and then executes any .sh scripts in /etc/profile.d/:
# /etc/profile snippet
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
[ -r "$i" ] && . "$i"
done
fiThe user’s ~/.profile includes ~/.bashrc if it exists:
# ~/.profile snippet
if [ -n "$BASH_VERSION" ]; then
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi ~/.profileis read once at login, while ~/.bashrc is read for each interactive shell.
Additional Tips
Create a custom file (e.g., uusama.profile) with your own variables and source it from ~/.profile for project‑specific settings.
Define command aliases in ~/.profile, such as alias rm="rm -i", to make 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.
