Operations 11 min read

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

This guide explains how to configure Linux environment variables—especially PATH for MySQL—using six different methods, demonstrates how to read variables, and reveals the exact order in which system and user files are loaded on Ubuntu.

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 add directories to the PATH or other environment variables. The article lists several ways to configure them on an Ubuntu 14.0 system (user uusama) with the MySQL binary path /home/uusama/mysql/bin.

Reading Environment Variables

export

– displays all currently defined environment variables. echo $PATH – prints the current 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:/bin

The PATH variable defines the search order for executable files, with entries separated by colons ( :). It can be modified with export, optionally quoting the value.

Method 1: Direct export PATH

export PATH=/home/uusama/mysql/bin:$PATH   # prepend
# or
export PATH=$PATH:/home/uusama/mysql/bin   # append

Effective immediately.

Only affects the current terminal session.

Applies to the current user.

Remember to keep the original $PATH to avoid overwriting existing entries.

Method 2: Edit ~/.bashrc

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

Effective when a new terminal is opened or after running source ~/.bashrc.

Permanent for the user.

Only the current user is affected.

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 login shell or after source ~/.bash_profile.

Permanent for the user.

Only the current user is affected.

If the file does not exist, edit ~/.profile instead.

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

# Make the file writable if necessary
chmod -v 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 until the file is changed again.

Applies to all users.

Method 5: Edit /etc/profile (system‑wide)

# Make the file writable if necessary
chmod -v u+w /etc/profile
vim /etc/profile
# add at the end
export PATH=$PATH:/home/uusama/mysql/bin

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

Permanent until the file is changed again.

Applies to all users.

Method 6: Edit /etc/environment (system‑wide)

# Make the file writable if necessary
chmod -v u+w /etc/environment
vim /etc/environment
# add at the end
export PATH=$PATH:/home/uusama/mysql/bin

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

Permanent until the file is changed again.

Applies to all users.

How Linux Loads Environment Variables

Environment variables are defined in two categories: user‑level files and system‑level files.

User‑level: ~/.bashrc, ~/.profile (or ~/.bash_profile).

System‑level: /etc/bashrc, /etc/profile (or /etc/bash_profile), /etc/environment.

The system reads these files in a specific order, which can cause later definitions to overwrite earlier ones.

Testing the Loading Order

To observe the order, the same variable UU_ORDER is added as the first line of each file, appending the file’s absolute path to its current value. After reopening a terminal, echo $UU_ORDER yields:

$UU_ORDER:/etc/environment:/etc/profile:/etc/bash.bashrc:/etc/profile.d/test.sh:~/.profile:~/.bashrc

From this output the loading sequence is inferred as:

/etc/environment

/etc/profile

/etc/bash.bashrc

/etc/profile.d/test.sh

~/.profile (or ~/.bash_profile)

~/.bashrc

File Loading Details

/etc/profile

sources /etc/bash.bashrc and then iterates over /etc/profile.d/*.sh. The user‑level ~/.profile subsequently sources ~/.bashrc. Consequently, system files are processed first, followed by user files.

Additional Tips

Create a custom environment file (e.g., uusama.profile) with a series of export statements and source it from ~/.profile for project‑specific variables.

Define command aliases such as alias rm="rm -i" in ~/.profile to make destructive commands safer.

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.

LinuxBashEnvironment Variables
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.