Master Linux Environment Variables: Six Ways to Set and Load PATH
This guide explains multiple methods for configuring Linux environment variables—especially PATH—on Ubuntu, covering user‑level files like ~/.bashrc and system‑wide files such as /etc/profile, and details the exact loading order Linux follows when initializing shells.
Linux Environment Variable Configuration
When installing software you often need to set environment variables. Below are several methods to configure them on Ubuntu 14.04 for user uusama, adding MySQL's /home/uusama/mysql/bin to PATH.
Reading Environment Variables
Use export to list all variables and echo $PATH to show 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 LESSCLOSE="/usr/bin/lesspipe %s %s"
declare -x LESSOPEN="| /usr/bin/lesspipe %s"
declare -x LOGNAME="uusama"
declare -x MAIL="/var/mail/uusama"
declare -x PATH="/home/uusama/bin:/home/uusama/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
declare -x SSH_TTY="/dev/pts/0"
declare -x TERM="xterm"
declare -x USER="uusama"
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 contains colon‑separated directories searched for executables. When using export you may enclose the value in quotes or not.
Method 1: export PATH directly
export PATH=/home/uusama/mysql/bin:$PATH
# or prepend
export PATH=$PATH:/home/uusama/mysql/binEffective immediately
Only for the current terminal session
Applies to the current user
Remember to include the original $PATH to avoid overwriting existing 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 affects the current user
May be overridden by later files that reset
PATHMethod 3: edit ~/.bash_profile
vim ~/.bash_profile
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective after opening a new terminal or source ~/.bash_profile Permanent for the user
If ~/.bash_profile does not exist, edit ~/.profile instead
Method 4: edit /etc/bashrc (system‑wide)
# make file writable if needed
chmod -v u+w /etc/bashrc
vim /etc/bashrc
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective in new terminals or after source /etc/bashrc Permanent
Applies to all users
Method 5: edit /etc/profile
# make file writable if needed
chmod -v u+w /etc/profile
vim /etc/profile
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective in new terminals or after source /etc/profile Permanent
System‑wide
Method 6: edit /etc/environment
# make file writable if needed
chmod -v u+w /etc/environment
vim /etc/environment
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective in new terminals or after source /etc/environment Permanent
System‑wide
How Linux Loads Environment Variables
Linux reads configuration files in a specific order, which determines which definition wins when the same variable appears multiple times.
Classification
User‑level files: ~/.bashrc, ~/.profile (or ~/.bash_profile)
System‑level files: /etc/bashrc, /etc/profile (or /etc/bash_profile), /etc/environment During login the shell reads ~/.bash_profile (or ~/.profile), then ~/.bashrc. If the former is missing, it falls back to ~/.bash_login.
Testing Load Order
By adding a line export UU_ORDER="$UU_ORDER:/path/to/file" to the first line of each file and then opening a new shell, the resulting value shows the load sequence:
$ echo $UU_ORDER
$UU_ORDER:/etc/environment:/etc/profile:/etc/bash.bashrc:/etc/profile.d/test.sh:~/.profile:~/.bashrcThe observed order is:
/etc/environment
/etc/profile
/etc/bash.bashrc
/etc/profile.d/test.sh
~/.profile
~/.bashrc
Detailed File Loading
/etc/profilesources /etc/bash.bashrc and then iterates over /etc/profile.d/*.sh. The user’s ~/.profile subsequently sources ~/.bashrc. The ~/.profile is read only once at login, while ~/.bashrc is read for each interactive shell.
Tips
Create a project‑specific file (e.g., uusama.profile) and source it from ~/.profile to load custom variables automatically.
Define command aliases with 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.
