Master Linux Environment Variables: Permanent, User, and Temporary Settings Explained
Learn how to define, modify, and apply Linux environment variables permanently for all users or individually, as well as temporary session variables, with clear examples of editing /etc/profile, .bash_profile, .bashrc, and using export, echo, and other common commands.
What Are Linux Environment Variables?
Linux is a multi‑user operating system, and each user has a personal runtime environment defined by a set of environment variables. Modifying these variables lets you tailor the environment to your needs.
Permanent Variables for All Users
Variables placed in /etc/profile apply system‑wide, affecting every user. Only the root account can edit this file.
# vi /etc/profile
export CLASSPATH=./JAVA_HOME/lib:$JAVA_HOME/jre/libAfter editing, run source /etc/profile to apply changes immediately; otherwise they take effect at the next login.
Permanent Variables for a Single User
For user‑specific settings, add export lines to ~/.bash_profile (login shells) or ~/.bashrc (non‑login shells). Both files are hidden; list them with ls -a.
# vi /home/rethink/.bash_profile
export CLASSPATH=./JAVA_HOME/lib:$JAVA_HOME/jre/lib
source /home/rethink/.bash_profileThe two files differ: .bash_profile is read once at login, while .bashrc is read for each new terminal session.
Temporary Session Variables
Variables defined directly in the shell with export VAR=value exist only for the current session. They disappear when the terminal is closed or the user logs out.
$ export NAME="rethink"
$ echo $NAME
rethinkCommon Commands for Managing Variables
echo – prints the value of a variable, e.g., echo $NAME .
export – creates or updates a variable, e.g., export NAME='rethink' .
env – lists all environment variables for the current user.
set – shows all shell variables, including user‑defined ones.
unset – removes a variable, e.g., unset NAME .
readonly – makes a variable immutable; unset will no longer affect it.
Frequently Used Environment Variables
PATH – colon‑separated list of directories searched for executable files. Example:
# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/binTo prepend the current directory:
$ pwd
/root/docker/httpd
$ export PATH=$PATH:$PWD
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/docker/httpdHOME – the user's home directory.
$ echo $HOME
/home/rethinkHISTSIZE – number of commands stored in history (default 1000).
$ echo $HISTSIZE
1000
$ HISTSIZE=1001
$ echo $HISTSIZE
1001LOGNAME – the login name of the current user.
$ echo $LOGNAME
rethinkHOSTNAME – the system's host name.
$ echo $HOSTNAME
JDu4e00u53f7SHELL – path to the user's default shell.
$ echo $SHELL
/bin/bashSigned-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.
