Fundamentals 11 min read

Master Linux Environment Variables: Configuration Methods & Loading Order

This guide explains how to read, set, and persist Linux environment variables using various commands and configuration files, compares multiple configuration approaches, and details the exact sequence in which the system loads these files for both user‑level and system‑wide settings.

Open Source Linux
Open Source Linux
Open Source Linux
Master Linux Environment Variables: Configuration Methods & Loading Order

Linux Environment Variable Configuration

When installing custom software on Linux, you often need to configure environment variables. Below are several common methods for defining and persisting them.

Reading Environment Variables

export

– displays all currently defined environment variables. echo $PATH – prints the value of the PATH variable.

Example output of the two commands shows the current PATH and other variables.

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 files, separated by colons. You can modify it with export, optionally quoting the value.

Method 1: Direct export PATH

Use the export command to prepend or append a directory to PATH:

export PATH=/home/uusama/mysql/bin:$PATH   # prepend
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 existing $PATH to avoid overwriting previous entries.

Method 2: Edit ~/.bashrc

Add the export line to the end of ~/.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 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 end of the file:

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

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

Permanent for the user.

Only affects the current user.

If the file does not exist, edit ~/.profile or create a new one.

Method 4: Edit /etc/bashrc

System‑wide configuration requires root privileges. Make the file writable, then add the export line:

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

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

Permanent.

Applies to all users.

Method 5: Edit /etc/profile

Another system‑wide file, edited similarly:

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

Effective when a new terminal is opened or after running source /etc/profile.

Permanent.

Applies to all users.

Method 6: Edit /etc/environment

System‑wide environment file, also requires root:

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

Effective when a new terminal is opened or after running source /etc/environment.

Permanent.

Applies to all users.

Linux Environment Variable Loading Principle

Linux loads environment variables in a specific order, and later definitions can overwrite earlier ones.

Classification of Environment Variables

They are divided into user‑defined and system‑level variables.

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

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

When a user logs in, the shell reads ~/.bash_profile (or ~/.profile); if absent, it reads ~/.bash_login, then ~/.bashrc.

Testing the Loading Order

Define a test variable UU_ORDER in the first line of each file, appending the file name to its value. After opening a new terminal, echo $UU_ORDER shows the concatenated order, revealing the loading sequence:

/etc/environment

/etc/profile

/etc/bash.bashrc

/etc/profile.d/test.sh

~/.profile

~/.bashrc

Detailed File Loading

/etc/profile

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

Additional Tips

You can create a custom environment file (e.g., uusama.profile) and source it from ~/.profile to load project‑specific variables on each login.

Aliases can also be defined, such as alias rm="rm -i", by adding them to ~/.profile for convenient 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 configurationEnvironment Variablespath
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.