Master Linux Environment Variables: Quick Config & Loading Order Explained
This guide explains how to configure Linux environment variables—especially PATH for MySQL—using various methods such as export commands, editing ~/.bashrc, ~/.bash_profile, /etc/bashrc, /etc/profile, and /etc/environment, and details the exact order Linux reads these files during login and shell startup.
Linux Environment Variable Configuration
When installing software manually, you often need to set environment variables. Below are several methods to configure them on Ubuntu 14.0 for user "uusama", adding the MySQL bin directory to PATH.
Reading Environment Variables
Use export to list all variables and echo $PATH to show the 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; you can add new ones with or without quotes.
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
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 for the current user
If later files overwrite PATH, this may not take effect
Method 3: Edit ~/.bash_profile
vim ~/.bash_profile
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective on new terminals or after source ~/.bash_profile Permanent for the user
Only for the current user
If ~/.bash_profile does not exist, edit ~/.profile or create it
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
Applies to all users
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
Applies to all users
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 When a login shell starts, Bash reads ~/.bash_profile (or ~/.profile), then ~/.bashrc. System files are read earlier.
Testing Load Order
By adding a line export UU_ORDER="$UU_ORDER:/path/to/file" at the top of each file and echoing $UU_ORDER in a new session, the observed order is:
/etc/environment
/etc/profile
/etc/bash.bashrc
/etc/profile.d/test.sh
~/.profile
~/.bashrc
Detailed Loading Process
/etc/profileloads /etc/bash.bashrc and then scripts in /etc/profile.d/*.sh. Afterwards, ~/.profile includes ~/.bashrc if it exists. The ~/.profile file is read 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 on each login.
Define command aliases (e.g., alias rm="rm -i") in ~/.profile for convenient usage.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
