Master Linux Environment Variables: How to Set, Update, and Use Them
This guide explains Linux environment variables, covering system‑wide and user‑specific permanent settings, temporary shell variables, common variables like PATH and HOME, and essential commands for viewing, modifying, and managing them effectively.
Linux is a multi‑user operating system, and each user has a personal environment defined by a set of environment variables that can be modified to suit individual needs.
How to Set Environment Variables
Permanent variables that affect all users
These variables are effective system‑wide; only the root user can modify the corresponding file.
# vi /etc/profile
export CLASSPATH=./JAVA_HOME/lib:$JAVA_HOME/jre/libAfter adding the variable, run source /etc/profile to apply it immediately; otherwise it takes effect at the next login.
Permanent variables that affect a single user
Add variables to the hidden .bash_profile file in the user’s home directory (visible with ls -a).
$ whoami
rethink
$ vi /home/rethink/.bash_profile
export CLASSPATH=./JAVA_HOME/lib:$JAVA_HOME/jre/lib
$ source /home/rethink/.bash_profileThe files .bash_profile (executed on login shells) and .bashrc (executed on each new interactive non‑login shell) can both be used; the former is read once at login, the latter at every new terminal session.
Temporary variables are valid only for the current shell session and disappear when the shell exits.
export NAME="rethink"
echo $NAME
rethinkCommon Environment Variables
PATH
# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/binThe PATH variable lists directories separated by colons where the shell searches for executable programs.
$ 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
$ whoami
rethink
echo $HOME
/home/rethinkHISTSIZE – number of commands saved in history (default 1000)
echo $HISTSIZE
1000
HISTSIZE=1001
echo $HISTSIZE
1001History commands are stored in memory and can be viewed with history. Use !n to re‑execute command number n.
history 5
59 ls
60 who
61 history | head -n 5
62 who am i
63 history 5
!62
who am i
root pts/0 2018-04-04 11:00 (101.81.92.18)LOGNAME – current login name
echo $LOGNAME
rethinkHOSTNAME – host name of the machine
echo $HOSTNAME
JDu4e00u53f7SHELL – path to the current 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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
