Master Linux Environment Variables: 6 Proven Configuration Methods
This guide explains why and how to configure Linux environment variables, demonstrates reading them, and provides six practical methods—including using export, editing ~/.bashrc, ~/.bash_profile, /etc/bashrc, /etc/profile, and /etc/environment—plus a detailed analysis of the loading order and useful tips for custom profiles and aliases.
Linux Environment Variable Configuration
When installing custom software you often need to set environment variables. The examples assume an Ubuntu 14.0 system, user uusama, and a MySQL binary directory at /home/uusama/mysql/bin.
Reading Environment Variables
export– lists all currently defined environment variables. echo $PATH – prints the value of the PATH variable.
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:/binThe PATH variable defines the search order for executable commands, separated by colons. It can be defined with or without surrounding quotes.
Method 1: Direct export PATH
Use the export command to modify PATH directly:
export PATH=/home/uusama/mysql/bin:$PATH # prepend MySQL bin
# or
export PATH=$PATH:/home/uusama/mysql/bin # append MySQL binEffective immediately.
Only lasts for the current terminal session.
Applies only to the current user.
Remember to include the original $PATH to avoid overwriting existing entries.
Method 2: Edit ~/.bashrc
Add the export line to the end of ~/.bashrc:
vim ~/.bashrc
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective when a new terminal is opened, or immediately after running source ~/.bashrc.
Persists permanently for the user.
Only affects the current user.
If another file later overwrites PATH, the change may be lost.
Method 3: Edit ~/.bash_profile (or ~/.profile )
Similar to ~/.bashrc, add the export line to the end of the file:
vim ~/.bash_profile
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective after opening a new login shell or running source ~/.bash_profile.
Permanent for the user.
If ~/.bash_profile does not exist, edit ~/.profile instead.
Method 4: Edit /etc/bashrc (system‑wide)
Requires root privileges. Make the file writable, then add the export line:
chmod -v u+w /etc/bashrc
vim /etc/bashrc
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective in new terminals or after source /etc/bashrc.
Permanent.
Applies to all users.
Method 5: Edit /etc/profile
Also system‑wide and requires root. The steps mirror Method 4:
chmod -v u+w /etc/profile
vim /etc/profile
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective in new terminals or after source /etc/profile.
Permanent.
Applies to all users.
Method 6: Edit /etc/environment
This file is another system‑wide configuration point:
chmod -v u+w /etc/environment
vim /etc/environment
# add at the end
export PATH=$PATH:/home/uusama/mysql/binEffective in new terminals or after source /etc/environment.
Permanent.
Applies to all users.
How Linux Loads Environment Variables
Environment variables are defined either at the user level or the system level.
User‑level files: ~/.bashrc, ~/.profile (or ~/.bash_profile).
System‑level files: /etc/bashrc, /etc/profile, /etc/bash_profile, /etc/environment.
During a login, the shell reads ~/.bash_profile (or ~/.profile); if absent it reads ~/.bash_login, then ~/.bashrc.
Testing the Loading Order
Insert the same test variable UU_ORDER at the top of each file, appending the file name to its value. After opening a new terminal and running echo $UU_ORDER, the observed order is:
UU_ORDER:/etc/environment:/etc/profile:/etc/bash.bashrc:/etc/profile.d/test.sh:~/.profile:~/.bashrc/etc/environment
/etc/profile
/etc/bash.bashrc
/etc/profile.d/test.sh
~/.profile
~/.bashrc
Detailed Loading Process
/etc/profileloads /etc/bash.bashrc and then iterates over /etc/profile.d/*.sh. Afterwards, ~/.profile loads ~/.bashrc. The ~/.profile file is read only once at login, while ~/.bashrc is read for each interactive shell.
Additional Tips
Create a custom file such as uusama.profile, define variables with export, and source it from ~/.profile for project‑specific settings.
Define command aliases, e.g., alias rm="rm -i", and place them in ~/.profile for convenient, safer usage.
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.
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.)
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.
