Operations 12 min read

Master Linux Environment Variables: Configuring Paths & Loading Order

This guide explains how to read, set, and permanently configure Linux environment variables—especially the PATH variable—using commands like export and editing files such as ~/.bashrc, ~/.profile, /etc/profile, /etc/bashrc, and /etc/environment, and details the loading order and practical tips for effective management.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Linux Environment Variables: Configuring Paths & Loading Order

Linux Environment Variable Configuration

When installing software manually, you often need to set environment variables. Below are various methods to configure them.

Reading Linux Environment Variables

Commands to read 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"
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 defines the search path for executable commands, separated by colons. You can add double quotes when using export, but they are optional.

Method 1: Directly export PATH

Use the export command to modify PATH, for example to add MySQL binaries:

export PATH=/home/uusama/mysql/bin:$PATH
# or put the new path at the front
export PATH=$PATH:/home/uusama/mysql/bin

Effective immediately.

Effective only for the current terminal session.

Effective for the current user only.

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 at the last line
export PATH=$PATH:/home/uusama/mysql/bin

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

Permanent.

Effective for the current user only.

If later files overwrite PATH, the change may not take effect.

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

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

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

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

Permanent.

Effective for the current user only.

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

Method 4: Edit /etc/bashrc

System‑wide configuration requires root privileges:

# make the file writable if needed
chmod -v u+w /etc/bashrc

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

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

Permanent.

Effective for all users.

Method 5: Edit /etc/profile

Another system‑wide file, also needs root privileges:

# make the file writable if needed
chmod -v u+w /etc/profile

vim /etc/profile
# add at the last line
export PATH=$PATH:/home/uusama/mysql/bin

Effective in new terminals or after source /etc/profile.

Permanent.

Effective for all users.

Method 6: Edit /etc/environment

System environment file, also requires root privileges:

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

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

Effective in new terminals or after source /etc/environment.

Permanent.

Effective for all users.

How Linux Loads Environment Variable Files

Environment variables are divided into user‑defined and system‑level. System files include /etc/bashrc, /etc/profile, /etc/environment; user files include ~/.bashrc, ~/.profile (or ~/.bash_profile).

When a login occurs, the shell reads ~/.bash_profile (or ~/.profile), then ~/.bashrc. If the former does not exist, it reads ~/.bash_login, and finally ~/.bashrc.

Testing the Loading Order

To observe the order, add a test variable UU_ORDER to the first line of each file, appending the file name to its value. After opening a new shell and running echo $UU_ORDER, you will see the concatenated list, 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 any .sh scripts in /etc/profile.d. The ~/.profile file subsequently sources ~/.bashrc, which is read for each interactive shell.

# /etc/profile snippet showing loading of /etc/bash.bashrc and /etc/profile.d/*.sh
if [ "PS1" ]; then
  if [ "BASH" ] && [ "BASH" != "/bin/sh" ]; then
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1=' '
    fi
  fi
fi

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r i ]; then
      . i
    fi
  done
  unset i
fi

The ~/.profile file includes:

# include .bashrc if it exists
if [ -n "BASH_VERSION" ]; then
  if [ -f "HOME/.bashrc" ]; then
    . "HOME/.bashrc"
  fi
fi
# set PATH to include user's private bin directories
PATH="HOME/bin:HOME/.local/bin:PATH"

Note that ~/.profile is read only once at login, while ~/.bashrc is read for each new shell.

Tips

Create a custom environment file for a project and source it from ~/.profile to have project‑specific variables available on login.

Define command aliases (e.g., alias rm="rm -i") in ~/.profile for safer command 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.

ShellEnvironment Variablespath
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.