Master Linux Environment Variables: Configuring, Reading, and Loading Order
This guide explains how to configure Linux environment variables using various methods such as export, editing ~/.bashrc, ~/.bash_profile, and system-wide files, demonstrates how to read them, and details the precise order in which Linux loads these configuration files.
Linux Environment Variable Configuration
When installing software, you often need to set environment variables; below are various methods.
Examples assume:
System: Ubuntu 14.0
Username: uusama
MySQL path: /home/uusama/mysql/bin
Linux Reading Environment Variables
Methods to read:
export command shows all defined variables
echo $PATH prints the current PATH value
Example output:
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 colons; quotes are optional.
Method 1: export PATH
Directly modify PATH with export:
export PATH=/home/uusama/mysql/bin:$PATH
# or put PATH first
export PATH=$PATH:/home/uusama/mysql/binNotes:
Effective immediately
Only for the current terminal session
Applies to the current user only
Remember to include the existing $PATH to avoid overwriting
Method 2: vim ~/.bashrc
Edit ~/.bashrc and add at the end:
vim ~/.bashrc
# add at the end
export PATH=$PATH:/home/uusama/mysql/binNotes:
Effective when opening a new terminal or after running source ~/.bashrc Permanent
Applies to the current user only
May be overridden by later files
Method 3: vim ~/.bash_profile
Similar to ~/.bashrc, edit ~/.bash_profile (or ~/.profile) and add:
vim ~/.bash_profile
# add at the end
export PATH=$PATH:/home/uusama/mysql/binNotes:
Effective on new terminal or after source ~/.bash_profile Permanent
Current user only
If the file is missing, edit ~/.profile or create one
Method 4: vim /etc/bashrc
System-wide configuration requires root:
# make /etc/bashrc writable
chmod -v u+w /etc/bashrc
vim /etc/bashrc
# add at the end
export PATH=$PATH:/home/uusama/mysql/binNotes:
Effective in new terminals or after source /etc/bashrc Permanent
Applies to all users
Method 5: vim /etc/profile
Similar to /etc/bashrc:
# make /etc/profile writable
chmod -v u+w /etc/profile
vim /etc/profile
# add at the end
export PATH=$PATH:/home/uusama/mysql/binNotes:
Effective in new terminals or after source /etc/profile Permanent
All users
Method 6: vim /etc/environment
System environment file:
# make /etc/environment writable
chmod -v u+w /etc/environment
vim /etc/environment
# add at the end
export PATH=$PATH:/home/uusama/mysql/binNotes:
Effective in new terminals or after source /etc/environment Permanent
All users
How Linux Loads Environment Variables
Variables are loaded in a specific order; later definitions can override earlier ones.
Classification of Environment Variable Files
User-level files: ~/.bashrc, ~/.profile (or ~/.bash_profile)
System-level files: /etc/bashrc, /etc/profile (or /etc/bash_profile), /etc/environment
Login shells read ~/.bash_profile (or ~/.profile) first, then ~/.bashrc; non‑login shells read ~/.bashrc directly.
Testing Load Order
Define a variable UU_ORDER in each file, appending the file name, then inspect its value. export UU_ORDER="$UU_ORDER:~/.bash_profile" After opening a new terminal, echo $UU_ORDER shows the order:
$UU_ORDER:/etc/environment:/etc/profile:/etc/bash.bashrc:/etc/profile.d/test.sh:~/.profile:~/.bashrcObserved load order:
/etc/environment
/etc/profile
/etc/bash.bashrc
/etc/profile.d/test.sh
~/.profile
~/.bashrc
Detailed File Loading
/etc/profile loads /etc/bash.bashrc and scripts in /etc/profile.d/*.sh:
# /etc/profile: system-wide .profile file for the Bourne shell
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 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
PATH="HOME/bin:HOME/.local/bin:PATH".profile is read once at login; .bashrc is read for each shell invocation.
Tips
Create a custom profile file (e.g., uusama.profile) with exports and source it from ~/.profile to load project‑specific variables automatically.
Define command aliases, such as alias rm="rm -i", and add them to ~/.profile for convenient safe deletions.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
