Operations 11 min read

Master Linux Environment Variables: Configuration Methods and Loading Order Explained

This guide explains how to configure Linux environment variables—especially PATH—using export commands, editing user and system profile files, and demonstrates the exact order in which Ubuntu loads these files, helping you avoid conflicts and ensure persistent settings.

ITPUB
ITPUB
ITPUB
Master Linux Environment Variables: Configuration Methods and Loading Order Explained

Linux Environment Variable Configuration

When installing software on Linux you often need to add directories to the PATH or other variables. The article lists several ways to set environment variables on Ubuntu 14.04 for user uusama, using the MySQL bin directory /home/uusama/mysql/bin as an example.

Reading environment variables

Use export to list all variables and echo $PATH to display the current PATH. Example output shows the default PATH composition.

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

Method 1 – Direct export

Add the new directory to PATH with a one‑line command:

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

Effective immediately in the current terminal.

Only affects the current user session.

Remember to include $PATH to avoid overwriting existing entries.

Method 2 – Edit ~/.bashrc

Append the export line to the end of ~/.bashrc and reload with source ~/.bashrc or open a new terminal.

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

Effective for new terminals of the same user.

Persistent across sessions.

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

Same export line added to the file; takes effect after login or source ~/.bash_profile.

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

Effective after login; permanent.

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

Method 4 – System‑wide /etc/bashrc

Requires root privileges. After making the file writable, add the export line.

chmod -v u+w /etc/bashrc
vim /etc/bashrc
export PATH=$PATH:/home/uusama/mysql/bin

Effective for all users after a new terminal or source /etc/bashrc.

Permanent.

Method 5 – System‑wide /etc/profile

Similar to the previous method; edit after ensuring write permission.

chmod -v u+w /etc/profile
vim /etc/profile
export PATH=$PATH:/home/uusama/mysql/bin

Effective for all users after a new terminal or source /etc/profile.

Permanent.

Method 6 – System‑wide /etc/environment

Also requires root. Add the export line at the end of the file.

chmod -v u+w /etc/environment
vim /etc/environment
export PATH=$PATH:/home/uusama/mysql/bin

Effective for all users after a new terminal or source /etc/environment.

Permanent.

How Linux Loads Environment Variables

Variables are divided into user‑level (e.g., ~/.bashrc, ~/.profile, ~/.bash_profile) and system‑level files ( /etc/bashrc, /etc/profile, /etc/environment). During login the shell reads files in a defined order, and later interactive shells read additional files.

Testing the load order

By inserting a test variable UU_ORDER that appends the file name in each configuration file, the resulting value after a new login shows the actual sequence:

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

/etc/environment

/etc/profile

/etc/bash.bashrc

/etc/profile.d/*.sh (e.g., test.sh)

~/.profile (or ~/.bash_profile)

~/.bashrc

Detailed file loading

/etc/profile

sources /etc/bash.bashrc and then any scripts in /etc/profile.d/. The user’s ~/.profile subsequently sources ~/.bashrc. The ~/.bashrc is read for each non‑login interactive shell, while ~/.profile is read only once at login.

Additional tips

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

Define command aliases with 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.

LinuxShellsystem configurationpath
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

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.