Operations 11 min read

Master Linux Environment Variables: 6 Proven Configuration Methods & Loading Order

Learn how to configure Linux environment variables using six practical methods—including export, editing ~/.bashrc, ~/.bash_profile, /etc/bashrc, /etc/profile, and /etc/environment—plus understand the loading sequence and testing techniques to ensure your PATH and custom variables work reliably across users and sessions.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Environment Variables: 6 Proven Configuration Methods & Loading Order

Linux Environment Variable Configuration

When installing software manually you often need to set environment variables. The examples assume Ubuntu 14.04, user uusama, and need to add the MySQL bin directory to PATH.

Reading Environment Variables

The export command lists all defined variables. echo $PATH shows the current PATH value.

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:/bin
PATH

is a colon‑separated list of directories searched for executables; quotes are optional when using export.

Method 1: export PATH

export PATH=/home/uusama/mysql/bin:$PATH
# or put the new path first
export PATH=$PATH:/home/uusama/mysql/bin

Effective immediately, but only for the current terminal session.

Scope limited to the current user.

Remember to include the existing $PATH to avoid overwriting.

Method 2: edit ~/.bashrc

vim ~/.bashrc
# add at the end
export PATH=$PATH:/home/uusama/mysql/bin

Effective for new terminals of the same user, or after running source ~/.bashrc.

Permanent for that user.

Only affects the current user.

If later files overwrite PATH, the change may be lost.

Method 3: edit ~/.bash_profile (or ~/.profile)

vim ~/.bash_profile
# add at the end
export PATH=$PATH:/home/uusama/mysql/bin

Effective after opening a new terminal or sourcing the file.

Permanent for that user.

If ~/.bash_profile does not exist, edit or create ~/.profile instead.

Method 4: edit /etc/bashrc (system‑wide)

# make the file writable if needed
chmod u+w /etc/bashrc
vim /etc/bashrc
# add at the end
export PATH=$PATH:/home/uusama/mysql/bin

Effective for new terminals or after source /etc/bashrc.

Permanent.

Applies to all users.

Method 5: edit /etc/profile

# make writable if needed
chmod u+w /etc/profile
vim /etc/profile
# add at the end
export PATH=$PATH:/home/uusama/mysql/bin

Effective for new terminals or after sourcing the file.

Permanent.

System‑wide for all users.

Method 6: edit /etc/environment

# make writable if needed
chmod u+w /etc/environment
vim /etc/environment
# add at the end
export PATH=$PATH:/home/uusama/mysql/bin

Effective for new terminals or after sourcing the file.

Permanent.

System‑wide for all users.

How Linux Loads Environment Variables

Variables are read in a specific order; later definitions can overwrite earlier ones.

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 echoing $UU_ORDER after a new login, the observed order is:

/etc/environment

/etc/profile

/etc/bash.bashrc

/etc/profile.d/test.sh (if present)

~/.profile

~/.bashrc

File Details

/etc/profile

sources /etc/bash.bashrc and then iterates over scripts in /etc/profile.d/*.sh. The user’s ~/.profile includes ~/.bashrc if a Bash session is detected, and may prepend user‑specific bin directories to PATH.

Additional Tips

Create a project‑specific file (e.g., uusama.profile) and source it from ~/.profile to load custom variables automatically.

Define command aliases such as alias rm="rm -i" in ~/.profile for safer usage.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxShellBashEnvironment Variablespath
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.