Operations 12 min read

Master Linux Environment Variable Configuration: Methods, Loading Order & Tips

This guide explains how to configure Linux environment variables using various methods—including direct export, editing ~/.bashrc, ~/.profile, /etc/bashrc, /etc/profile, and /etc/environment—covers the loading order of these files, provides testing techniques, and offers practical tips for effective variable management.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Environment Variable Configuration: Methods, Loading Order & Tips

Linux Environment Variable Configuration

When installing custom software you often need to add directories to the PATH or other environment variables. The following examples show several ways to define and modify environment variables on an Ubuntu 14.04 system (user uusama) and how to verify them.

Reading environment variables

Two common commands are used: export – displays all currently defined environment variables. echo $PATH – prints the value of the PATH variable.

Example output:

uusama@ubuntu:~$ export
declare -x HOME="/home/uusama"
declare -x LANG="en_US.UTF-8"
declare -x LANGUAGE="en_US:"
declare -x LESSCLOSE="/usr/bin/lesspipe %s %s"
declare -x LESSOPEN="| /usr/bin/lesspipe %s"
declare -x LOGNAME="uusama"
declare -x MAIL="/var/mail/uusama"
declare -x PATH="/home/uusama/bin:/home/uusama/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
declare -x SSH_TTY="/dev/pts/0"
declare -x TERM="xterm"
declare -x USER="uusama"

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 executable files. When redefining it with export, the existing value can be referenced with $PATH and optionally wrapped in quotes.

Method 1 – Direct export

Modify PATH in the current shell:

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 for new terminals of the same user, or after running source ~/.bashrc.

Persists for the user.

If another file later redefines PATH, this change may be overridden.

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

Same as Method 2 but using ~/.bash_profile (or ~/.profile) which is read at login:

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

Effective after login or after source ~/.bash_profile.

Permanent for the user.

If ~/.bash_profile does not exist, ~/.profile can be used.

Method 4 – System‑wide /etc/bashrc

Requires root privileges. Make the file writable, then edit:

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

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

Permanent.

Applies system‑wide.

Method 5 – System‑wide /etc/profile

Similar to Method 4 but editing /etc/profile (or /etc/bash_profile on some distributions):

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

Effective after new login or source /etc/profile.

Permanent and system‑wide.

Method 6 – Edit /etc/environment

This file is read early by the PAM module. After making it writable, add the export line:

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

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

Permanent system‑wide setting.

How Linux Loads Environment Variable Files

Linux reads configuration files in a specific order, which determines which definition wins when the same variable appears multiple times.

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

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

When a login shell starts, it first reads ~/.bash_profile (or ~/.profile), then ~/.bashrc. If the former does not exist, ~/.bash_login is consulted. System files are sourced earlier: /etc/environment, then /etc/profile, which in turn sources /etc/bash.bashrc and any /etc/profile.d/*.sh scripts.

Testing the Loading Order

To observe the order, add the following line to the first line of each file, replacing the placeholder with the file’s absolute path:

export UU_ORDER="$UU_ORDER:~/.bash_profile"

After saving, open a new terminal and run echo $UU_ORDER. The resulting string shows the sequence in which the files were processed, e.g.:

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

From this experiment the loading order is:

/etc/environment

/etc/profile

/etc/bash.bashrc

/etc/profile.d/test.sh (if present)

~/.profile

~/.bashrc

File‑Loading Details

/etc/profile

loads /etc/bash.bashrc and then iterates over /etc/profile.d/*.sh. The user’s ~/.profile typically sources ~/.bashrc. ~/.profile is read once at login, while ~/.bashrc is read for each interactive non‑login shell.

Additional Tips

Create a project‑specific file such as uusama.profile, define variables with export, and source it from ~/.profile to keep your environment tidy.

Define command aliases (e.g., 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.

BashEnvironment 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.