Fundamentals 13 min read

Master Linux Environment Variables: Quick Configurations & Loading Order

This guide explains how to configure Linux environment variables using various methods such as export, editing ~/.bashrc, ~/.bash_profile, /etc/bashrc, /etc/profile, and /etc/environment, and details the loading sequence and practical tips for effective management.

Open Source Linux
Open Source Linux
Open Source Linux
Master Linux Environment Variables: Quick Configurations & Loading Order

Linux environment variable configuration

When installing software manually, you often need to set environment variables; the following lists several ways to do so.

System: Ubuntu 14.0

Username: uusama

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

Linux reading environment variables

Methods to read environment variables: export – shows all currently defined environment variables. echo $PATH – prints the value of the PATH variable.

Running these commands yields:

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 ( :). When defining it with export, quotes are optional.

Linux environment variable configuration method 1: export PATH

Use the export command to modify PATH directly, e.g., to add MySQL:

export PATH=/home/uusama/mysql/bin:$PATH
# or put PATH first
export PATH=PATH:/home/uusama/mysql/bin

Notes:

Effective time: immediate

Effective duration: only the current terminal session

Scope: current user only

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

Linux environment variable configuration method 2: vim ~/.bashrc

Edit the user’s ~/.bashrc file:

vim ~/.bashrc

# add at the end
export PATH=$PATH:/home/uusama/mysql/bin

Notes:

Effective time: when a new terminal is opened or after running source ~/.bashrc Effective duration: permanent

Scope: current user only

If another startup file later overwrites PATH, this change may not take effect.

Linux environment variable configuration method 3: vim ~/.bash_profile

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

vim ~/.bash_profile

# add at the end
export PATH=$PATH:/home/uusama/mysql/bin

Notes:

Effective time: when a new terminal is opened or after running source ~/.bash_profile Effective duration: permanent

Scope: current user only

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

Linux environment variable configuration method 4: vim /etc/bashrc

This system‑wide method requires root or write permission:

# 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

Notes:

Effective time: after opening a new terminal or running source /etc/bashrc Effective duration: permanent

Scope: all users

Linux environment variable configuration method 5: vim /etc/profile

Another system‑wide file, edited similarly:

# 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

Notes:

Effective time: after opening a new terminal or running source /etc/profile Effective duration: permanent

Scope: all users

Linux environment variable configuration method 6: vim /etc/environment

System environment file, also requires root:

# 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

Notes:

Effective time: after opening a new terminal or running source /etc/environment Effective duration: permanent

Scope: all users

Linux environment variable loading principle analysis

Environment variables are defined either by the user or system‑wide files.

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

System‑level files: /etc/bashrc, /etc/profile (or /etc/bash_profile), /etc/environment When a user logs in, the shell reads ~/.bash_profile (or ~/.profile); if absent, it reads ~/.bash_login, then proceeds to ~/.bashrc.

Testing Linux environment variable loading order

To test the load order, add a variable UU_ORDER at the top of each file, appending the file name to its value:

/etc/environment

/etc/profile

/etc/profile.d/test.sh

/etc/bashrc (or /etc/bash.bashrc)

~/.bash_profile (or ~/.profile)

~/.bashrc export UU_ORDER="$UU_ORDER:/etc/environment" After saving, open a new terminal and run echo $UU_ORDER:

uusama@ubuntu:~$ echo $UU_ORDER
/etc/environment:/etc/profile:/etc/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

Linux environment variable file loading details

Overall order: system environment → user custom environment.

/etc/environment -> /etc/profile -> ~/.profile
/etc/profile

loads /etc/bash.bashrc and then any .sh scripts in /etc/profile.d:

# /etc/profile: system-wide .profile file for the Bourne shell (sh)
if [ "$PS1" ]; then
  if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    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 user’s ~/.profile includes ~/.bashrc if it exists:

# if running bash
if [ -n "${BASH_VERSION}" ]; then
  # include .bashrc if it exists
  if [ -f "HOME/.bashrc" ]; then
    . "HOME/.bashrc"
  fi
fi

# set PATH so it includes user's private bin directories
PATH="HOME/bin:HOME/.local/bin:PATH"
~/.profile

is read once at login, while ~/.bashrc is read for each new shell.

Some small tips

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

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

ConfigurationLinuxShellBashEnvironment Variablespath
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.