Operations 13 min read

Master Linux Environment Variables: 6 Proven Configuration Methods

This guide explains how to read and configure Linux environment variables, covering six methods—including export, editing ~/.bashrc, ~/.bash_profile, /etc/bashrc, /etc/profile, and /etc/environment—while detailing the loading order, user vs system scopes, and practical tips for managing PATH and custom variables.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Linux Environment Variables: 6 Proven Configuration Methods

Linux Environment Variable Configuration

When installing software manually, you often need to configure environment variables; below are various methods.

System: Ubuntu 14.0

Username: uusama

MySQL path to add: /home/uusama/mysql/bin

Reading Linux Environment Variables

Methods to read environment variables:

The export command displays all currently defined environment variables.

The echo $PATH command outputs the current 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. Quotes are optional when using export.

Method 1: Using export PATH

Directly modify PATH with export to add the MySQL directory:

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

Effective immediately.

Effective only for the current terminal session; closes when the window is closed.

Applies only to the current user.

Remember to include the original $PATH to avoid overwriting existing entries.

Method 2: Editing ~/.bashrc

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

Applies only to the current user.

If later files overwrite PATH, this setting may be ignored.

Method 3: Editing ~/.bash_profile

Similar to ~/.bashrc, add the export line to the end of ~/.bash_profile (or ~/.profile on some systems):

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

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

Permanent.

Applies only to the current user.

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

Method 4: Editing /etc/bashrc

This system‑wide method requires root privileges:

# make the file writable if needed
chmod -v 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: Editing /etc/profile

Another system‑wide file that also requires root privileges:

# make the file writable if needed
chmod -v 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: Editing /etc/environment

This file defines system‑wide environment variables and also needs root access:

# make the file writable if needed
chmod -v 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.

How Linux Loads Environment Variables

Linux reads configuration files in a specific order, and later definitions can overwrite earlier ones.

Classification of Environment Variables

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

System‑level files: /etc/bashrc, /etc/profile (or /etc/bash_profile), /etc/environment. The system first reads ~/.bash_profile (or ~/.profile), then ~/.bashrc if present.

Testing the Loading Order

To test the order, add the same variable UU_ORDER to the first line of each file, appending the file name to its value: export UU_ORDER="$UU_ORDER:~/.bash_profile" After editing all files, open a new terminal and run echo $UU_ORDER:

echo $UU_ORDER
# Example output:
$UU_ORDER:/etc/environment:/etc/profile:/etc/bash.bashrc:/etc/profile.d/test.sh:~/.profile:~/.bashrc

The observed loading sequence is:

/etc/environment

/etc/profile

/etc/bash.bashrc

/etc/profile.d/test.sh

~/.profile

~/.bashrc

Detailed File Loading Explanation

System files are read first, followed by user files. Opening /etc/profile shows it loads /etc/bash.bashrc and then scripts in /etc/profile.d. The ~/.profile file subsequently sources ~/.bashrc. The ~/.profile is read once at login, while ~/.bashrc is read for each new shell.

# /etc/profile: system‑wide profile file for Bourne‑compatible shells
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
# ~/.profile
if [ -n "BASH_VERSION" ]; then
  if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
  fi
fi
PATH="$HOME/bin:$HOME/.local/bin:$PATH"

Some Handy Tips

You can create a custom environment file (e.g., uusama.profile) with a series of export statements and source it from ~/.profile to make the variables available on every login.

Aliases can also be defined, such as alias rm="rm -i", and added to ~/.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.

Bashsystem configurationShell scriptingEnvironment 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.