Operations 11 min read

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

This guide walks through six practical ways to set Linux environment variables—including export commands, editing ~/.bashrc, ~/.bash_profile, /etc/bashrc, /etc/profile, and /etc/environment—explains how the system loads them, and provides testing tips and useful shell tricks.

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

Linux Environment Variable Configuration

When installing custom software on Linux you often need to add directories to the PATH or define other variables. The examples below assume Ubuntu 14.0, user uusama, and a MySQL binary directory /home/uusama/mysql/bin.

Reading Environment Variables

export

– lists all exported variables. echo $PATH – prints the current PATH value.

uusama@ubuntu:~$ export
declare -x HOME="/home/uusama"
declare -x LANG="en_US.UTF-8"
... 
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 is a colon‑separated list of directories that the shell searches for executables. It can be modified with or without surrounding quotes.

Method 1: export PATH

Directly prepend or append the new directory:

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 keep the original $PATH to avoid overwriting existing entries.

Method 2: Edit ~/.bashrc

Add the export line at the end of the file:

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

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

Permanent for the user.

May be overridden by later configuration files.

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

Same as the previous method, but placed in the login‑shell file:

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

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

Permanent for the user.

If the file does not exist, edit ~/.profile or create it.

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

Requires root privileges:

chmod -v u+w /etc/bashrc
vim /etc/bashrc
# at the last line
export PATH=$PATH:/home/uusama/mysql/bin

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

Permanent for all users.

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

Also requires root:

chmod -v u+w /etc/profile
vim /etc/profile
# at the last line
export PATH=$PATH:/home/uusama/mysql/bin

Effective for new login sessions or after source /etc/profile.

Permanent for all users.

Method 6: Edit /etc/environment

System‑wide environment file (root required):

chmod -v u+w /etc/environment
vim /etc/environment
# add or modify the line
PATH="$PATH:/home/uusama/mysql/bin"

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

Permanent for all users.

How Linux Loads Environment Variables

Variables are read from both user‑level and system‑level files. The typical loading order is:

/etc/environment

/etc/profile

/etc/bash.bashrc (or /etc/bashrc)

/etc/profile.d/*.sh

~/.profile (or ~/.bash_profile)

~/.bashrc

User‑level files are processed after system‑level ones, and ~/.bashrc is sourced on every interactive shell, while ~/.profile is read only at login.

Testing the Loading Order

To verify the order, add the same variable UU_ORDER to the first line of each file, appending the file name: export UU_ORDER="$UU_ORDER:/path/to/file" After opening a new terminal, echo $UU_ORDER will show the concatenated list, confirming the sequence shown above.

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.