Master Linux Environment Variables: 6 Methods & Loading Order Explained
This guide walks through six ways to configure Linux environment variables—including export commands, editing ~/.bashrc, ~/.bash_profile, /etc/bashrc, /etc/profile, and /etc/environment—while also detailing how the system loads these files and offering practical tips for testing and customizing variable definitions.
Linux Environment Variable Configuration
When installing custom software, configuring environment variables is often required; the following methods illustrate various ways to set them.
Example environment:
System: Ubuntu 14.0
Username: uusama
MySQL binary path to add: /home/uusama/mysql/bin
Reading Environment Variables in Linux
Common commands: export – displays all defined environment variables. echo $PATH – outputs 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 defines the directories searched for executable commands, separated by colons. Quotes are optional when using export.
Method 1: export PATH
Directly modify PATH with the export command:
export PATH=/home/uusama/mysql/bin:$PATH
# or prepend the new path
export PATH=$PATH:/home/uusama/mysql/binEffective immediately.
Only lasts 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
Add the export line to the end of ~/.bashrc:
vim ~/.bashrc
# Append 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.
If later files overwrite PATH, the change may not take effect.
Method 3: Edit ~/.bash_profile
Similar to ~/.bashrc, add the export line to ~/.bash_profile (or ~/.profile on some systems):
vim ~/.bash_profile
# Append 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.
Only affects the current user.
If ~/.bash_profile does not exist, edit ~/.profile or create a new file.
Method 4: Edit /etc/bashrc
System‑wide configuration requires root privileges:
# Make the file writable if needed
chmod -v u+w /etc/bashrc
vim /etc/bashrc
# Append 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, also requiring root:
# Make the file writable if needed
chmod -v u+w /etc/profile
vim /etc/profile
# Append 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 (requires root):
# Make the file writable if needed
chmod -v u+w /etc/environment
vim /etc/environment
# Append at the end
export PATH=$PATH:/home/uusama/mysql/binEffective for new terminals or after source /etc/environment.
Permanent.
Applies to all users.
Understanding Linux Environment Variable Loading Order
The system loads variables from several files in a specific sequence, which determines which definitions take precedence.
Variable Classification
Variables are either user‑level (e.g., ~/.bashrc, ~/.profile) or system‑level (e.g., /etc/bashrc, /etc/profile, /etc/environment).
User‑level files: ~/.bashrc, ~/.profile (or ~/.bash_profile).
System‑level files: /etc/bashrc, /etc/profile (or /etc/bash_profile), /etc/environment.
When a user logs in, the system reads ~/.bash_profile (or ~/.profile); if absent, it reads ~/.bash_login, and then processes ~/.bashrc.
Testing the Loading Sequence
To observe the order, add a test variable UU_ORDER to the first line of each file, appending the file name:
export UU_ORDER="$UU_ORDER:~/.bash_profile"After saving, open a new terminal and run echo $UU_ORDER:
echo $UU_ORDER
$UU_ORDER:/etc/environment:/etc/profile:/etc/bash.bashrc:/etc/profile.d/test.sh:~/.profile:~/.bashrcThe observed loading order is:
/etc/environment
/etc/profile
/etc/bash.bashrc
/etc/profile.d/test.sh
~/.profile
~/.bashrc
Detailed File Loading
/etc/profileloads /etc/bash.bashrc and then any .sh scripts in /etc/profile.d/:
# /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
fi ~/.profileincludes ~/.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"Note that ~/.profile is read once at login, while ~/.bashrc is read each time a new shell is started.
Additional Tips
You can create a custom environment file (e.g., uusama.profile) with multiple export statements and source it from ~/.profile to load project‑specific variables on each login.
Aliases can also be defined, such as alias rm="rm -i", and added to ~/.profile for convenient 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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
