Operations 12 min read

Master Linux Environment Variables: 6 Proven Configuration Methods

This guide explains how to read, set, and persist Linux environment variables—including PATH—using export commands, user‑level files like ~/.bashrc and ~/.bash_profile, and system‑wide files such as /etc/bashrc, /etc/profile, and /etc/environment, while also detailing the loading order and useful tips.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Environment Variables: 6 Proven Configuration Methods

Linux Environment Variable Basics

When installing custom software on Linux, configuring environment variables is often required. The examples assume Ubuntu 14.0, user uusama, and a MySQL binary directory at /home/uusama/mysql/bin.

Reading Environment Variables

Use export to list all currently defined environment variables.

Use echo $PATH to display the current PATH value.

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 executables, separated by colons. Quotes around the value are optional when using export.

Method 1: Direct export PATH

Modify PATH on the fly with:

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

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 following line at 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 the current user is affected.

If later files overwrite PATH, the change may be lost.

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

Similar to ~/.bashrc, add:

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.

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

Method 4: Edit System File /etc/bashrc

Requires root privileges. Make the file writable, then add the export line:

# make writable if needed
chmod -v 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 running source /etc/bashrc.

Permanent system‑wide change.

Applies to every user.

Method 5: Edit System File /etc/profile

Also requires root. The steps mirror those for /etc/bashrc:

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

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

Permanent system‑wide change.

Applies to every user.

Method 6: Edit /etc/environment

This file sets system‑wide environment variables. After gaining write permission, add the export line:

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

Effective after a new login or source /etc/environment.

Permanent for all users.

Applies system‑wide.

How Linux Loads Environment Variables

Linux reads configuration files in a specific order, and later definitions can overwrite earlier ones. The main categories are user‑level files ( ~/.bashrc, ~/.profile / ~/.bash_profile) and system‑level files ( /etc/bashrc, /etc/profile, /etc/environment).

Testing the Loading Order

To observe the order, add a line defining UU_ORDER in each file, appending the file’s name to the variable. Example for ~/.bash_profile: export UU_ORDER="$UU_ORDER:~/.bash_profile" After reopening a terminal, echo $UU_ORDER yields:

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

From this result, the loading sequence is:

/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

loads /etc/bash.bashrc and then iterates over scripts in /etc/profile.d/. The user’s ~/.profile subsequently sources ~/.bashrc. The ~/.bashrc file is read each time a new interactive shell starts, while ~/.profile is read only at login.

# /etc/profile snippet
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

In ~/.profile:

# include .bashrc if it exists
if [ -n "${BASH_VERSION}" ]; then
  if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
  fi
fi
PATH="$HOME/bin:$HOME/.local/bin:$PATH"

Additional Tips

Create a custom profile file (e.g., uusama.profile) with many export statements, then source it from ~/.profile for project‑specific variables.

Define command aliases, such as alias rm="rm -i", by adding them to ~/.profile or ~/.bashrc for convenient usage.

Linux environment variable diagram
Linux environment variable diagram
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
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.