Operations 13 min read

Master Linux Environment Variables: Configuration, Loading Order, and Tips

Learn how to configure Linux environment variables using various methods such as export, editing ~/.bashrc, ~/.profile, /etc/profile, and /etc/environment, understand the loading sequence of these files, explore classification of user and system variables, and discover practical tips and testing techniques.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Environment Variables: Configuration, Loading Order, and Tips

Linux Environment Variable Configuration

When installing software manually, you often need to set environment variables. Below are several ways to configure them on Linux.

System: Ubuntu 14.04

User: uusama

Target: add MySQL bin directory /home/uusama/mysql/bin to

PATH

Reading Environment Variables

You can list all environment variables with export and display a specific variable with echo $PATH.

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 directories the shell searches for executable files, separated by colons.

Method 1: Using export PATH

Directly modify PATH in the current shell:

export PATH=/home/uusama/mysql/bin:$PATH

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

Effective immediately

Only lasts for the current terminal session

Applies to the current user

Remember to keep the original $PATH part to avoid overwriting existing entries

Method 2: Editing ~/.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: Editing ~/.bash_profile

Similar to ~/.bashrc, add the export line to ~/.bash_profile (or ~/.profile if the former does not exist):

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

Effective after opening a new login shell or running source ~/.bash_profile Permanent for the user

Only affects the current user

Method 4: Editing /etc/bashrc

System‑wide configuration requires root privileges:

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

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

Applies to all users

Method 5: Editing /etc/profile

Another system‑wide file, edited similarly to /etc/bashrc:

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

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

Applies to all users

Method 6: Editing /etc/environment

This file sets system‑wide environment variables. Edit it with root rights:

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

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

Applies to all users

How Linux Loads Environment Variables

Linux reads configuration files in a specific order. The order determines which definition wins when the same variable is defined multiple times.

Classification of Environment Variables

They can be divided into:

User‑level variables defined in ~/.bashrc, ~/.profile (or ~/.bash_profile)

System‑level variables defined in /etc/bashrc, /etc/profile (or /etc/bash_profile), and /etc/environment When a login occurs, the shell reads ~/.bash_profile (or ~/.profile), then ~/.bashrc, applying the values in that sequence.

Testing the Loading Order

To see the order, add a unique variable UU_ORDER to the first line of each file, appending the file name:

export UU_ORDER="$UU_ORDER:/etc/bash_profile"

After logging out and back in, run echo $UU_ORDER to view the concatenated list, which reveals the loading sequence:

/etc/environment

/etc/profile

/etc/bash.bashrc

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

~/.profile

~/.bashrc

Detailed Loading Process

The system first processes /etc/profile, which may source /etc/bash.bashrc and scripts under /etc/profile.d/. Then ~/.profile is read, which in turn sources ~/.bashrc. The ~/.bashrc file is executed for each interactive shell.

# Example snippet from /etc/profile
if [ "$PS1" ]; then
  if [ -n "$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

Later, ~/.profile typically contains:

# Include .bashrc if it exists
if [ -n "$BASH_VERSION" ]; then
  if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
  fi
fi

# Ensure user bin directories are in PATH
PATH="$HOME/bin:$HOME/.local/bin:$PATH"

Additional Tips

You can create a custom environment file for a specific project, e.g., uusama.profile, define variables there, and source it from ~/.profile with source uusama.profile. This makes the variables available on each login.

Aliases can also simplify commands, such as alias rm="rm -i", which can be added to ~/.profile for persistent use.

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.

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