Master Linux Environment Variables: Configure, Read, and Understand Loading Order
This guide explains how to read and set Linux environment variables, presents multiple methods for configuring the PATH variable—including export, user‑level rc files, and system‑wide files—and details the exact order in which these files are loaded, helping you avoid conflicts and ensure proper persistence.
Linux Environment Variable Configuration
When installing software you often need to set environment variables; below are various methods.
System: Ubuntu 14.0
Username: uusama
Need to configure MySQL environment variable path: /home/uusama/mysql/bin
Reading Linux Environment Variables
Methods: export shows all defined environment variables. echo $PATH prints the current PATH value.
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:/binThe PATH variable defines command search paths separated by ':' and can be modified with export.
Method 1: export PATH
Directly modify PATH:
export PATH=/home/uusama/mysql/bin:$PATH
# or put PATH at the end
export PATH=$PATH:/home/uusama/mysql/binEffective immediately.
Only for the current terminal session.
Applies to the current user.
Remember to include the original $PATH to avoid overwriting.
Method 2: vim ~/.bashrc
Edit ~/.bashrc and add at the end:
vim ~/.bashrc
# add at the last line
export PATH=$PATH:/home/uusama/mysql/binEffective when a new terminal is opened or after source ~/.bashrc.
Permanent for the user.
Method 3: vim ~/.bash_profile
Similar to ~/.bashrc, edit and add the same line.
vim ~/.bash_profile
export PATH=$PATH:/home/uusama/mysql/binEffective after new login or source ~/.bash_profile.
Permanent for the user.
Method 4: vim /etc/bashrc
System‑wide configuration requires root:
# make file writable if needed
chmod -v u+w /etc/bashrc
vim /etc/bashrc
# add at the last line
export PATH=$PATH:/home/uusama/mysql/binEffective for all users after new terminal or source /etc/bashrc.
Method 5: vim /etc/profile
System‑wide configuration similar to /etc/bashrc:
chmod -v u+w /etc/profile
vim /etc/profile
export PATH=$PATH:/home/uusama/mysql/binEffective after new terminal or source /etc/profile.
Method 6: vim /etc/environment
Another system‑wide file requiring root:
chmod -v u+w /etc/environment
vim /etc/environment
export PATH=$PATH:/home/uusama/mysql/binEffective after new terminal or source /etc/environment.
Linux Environment Variable Loading Principles
Environment variables are loaded in a specific order, affecting which definition takes effect.
Classification
User‑level files: ~/.bashrc, ~/.profile (or ~/.bash_profile).
System‑level files: /etc/bashrc, /etc/profile, /etc/bash_profile, /etc/environment.
When a login shell starts, Bash reads ~/.bash_profile (or ~/.profile), then ~/.bashrc. System files are read earlier: /etc/environment → /etc/profile → /etc/bash.bashrc → files in /etc/profile.d → user files.
Testing Loading Order
By adding export UU_ORDER="$UU_ORDER:/path/to/file" at the top of each file and echoing $UU_ORDER, the observed order is:
/etc/environment
/etc/profile
/etc/bash.bashrc
/etc/profile.d/test.sh
~/.profile
~/.bashrc
File Loading Details
/etc/profile loads /etc/bash.bashrc and scripts in /etc/profile.d/*.sh. ~/.profile loads ~/.bashrc. ~/.bashrc is read for each non‑login shell.
Tips
Create a project‑specific file (e.g., uusama.profile) and source it from ~/.profile to load custom variables on login.
Define command aliases with alias in ~/.profile for convenient usage.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
