Operations 12 min read

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

This guide walks through six practical ways to set Linux environment variables—using export, editing ~/.bashrc, ~/.bash_profile, /etc/bashrc, /etc/profile, and /etc/environment—while detailing the exact loading sequence of these files and offering tips for testing and customizing variable definitions.

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

Linux Environment Variable Configuration

When installing custom software on Linux, configuring environment variables is often required. Below are several methods to set and persist environment variables, illustrated with a MySQL binary path example ( /home/uusama/mysql/bin).

Reading Environment Variables

export

– displays all currently defined environment variables. echo $PATH – prints the 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 directories the shell searches for executable commands, separated by colons ( :). It can be modified with export, optionally quoting the value.

Method 1: Direct export PATH in the Shell

Modify PATH on the fly:

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

Effective 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 at the end of ~/.bashrc:

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

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

Persists for the user across sessions.

Only affects the current user.

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

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

Similar to ~/.bashrc, add the export line at the file's end:

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

Effective after a new login shell or after source ~/.bash_profile.

Permanent for the user.

Only the current user is affected.

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

Method 4: Edit System File /etc/bashrc

Requires root or write permission:

# make 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 System File /etc/profile

Similar to the previous method:

# make file 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 source /etc/profile.

Permanent.

Applies to all users.

Method 6: Edit System File /etc/environment

This file also requires root permission:

# make file writable if needed
chmod u+w /etc/environment

vim /etc/environment
# add at the end
export PATH=$PATH:/home/uusama/mysql/bin

Effective after a new terminal or after source /etc/environment.

Permanent.

Applies to all users.

Linux Environment Variable Loading Principles

Environment variables are defined either by user‑level files ( ~/.bashrc, ~/.profile, ~/.bash_profile) or system‑level files ( /etc/bashrc, /etc/profile, /etc/environment). The shell reads them in a specific order, which can affect which definition wins.

Testing the Loading Order

To observe the order, add the same variable UU_ORDER to the first line of each file, appending the file name to its value:

export UU_ORDER="$UU_ORDER:/path/to/file"

After reopening a terminal, run echo $UU_ORDER. The resulting concatenated value reveals the sequence:

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

Derived Loading Sequence

/etc/environment

/etc/profile

/etc/bash.bashrc

/etc/profile.d/test.sh

~/.profile

~/.bashrc

File‑Level Details

/etc/profile loads /etc/bash.bashrc and then iterates over /etc/profile.d/*.sh. ~/.profile subsequently sources ~/.bashrc. The ~/.bashrc file is read each time a new interactive shell starts, while ~/.profile is read only once at login.

Useful Tips

Create a custom file (e.g., uusama.profile) with a series of export statements, then source it from ~/.profile to reuse variables across sessions.

Define command aliases with alias, such as alias rm="rm -i", and place them in ~/.profile for persistent convenience.

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.

LinuxShellsystem configuration
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.