Master Linux Environment Variables: Set, Update, and Use Them Effectively
This guide explains what Linux environment variables are, how they differ for all users, single users, and temporary sessions, and provides step‑by‑step commands to create, modify, and persist variables such as PATH, HOME, HISTSIZE, LOGNAME, and others.
Linux is a multi‑user operating system; each logged‑in user has a dedicated environment defined by environment variables that can be customized.
Methods to set environment variables
Permanent variables for all users
These variables apply system‑wide; they are defined in files that only root can modify.
# vi /etc/profile
export CLASSPATH=./JAVA_HOME/lib:$JAVA_HOME/jre/libAfter adding, run source /etc/profile to apply immediately; otherwise they take effect on next login.
Permanent variables for a single user
Add variables to the hidden .bash_profile (or .bashrc) in the user’s home directory.
# vi /home/rethink/.bash_profile
export CLASSPATH=./JAVA_HOME/lib:$JAVA_HOME/jre/lib
source /home/rethink/.bash_profile.bash_profile is read once at login, while .bashrc is read for each new interactive non‑login shell.
Thus .bash_profile runs only on login, whereas .bashrc runs on every terminal session.
Temporary variables (current shell only)
These exist only for the current shell session and disappear after logout or terminal close.
Set them directly with export VAR=value.
export NAME=example
echo $NAMECommon commands for environment variables
echo – display a variable, e.g., echo $NAME
export – create a new variable, e.g., export NAME='rethink'
Update – assign a new value, e.g., NAME='test'
env – list current user variables
set – list shell variables (includes user variables)
unset – remove a variable, e.g., unset NAME
readonly – make a variable read‑only; readonly NAME prevents unset
Common environment variables
PATH
# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/binPATH contains colon‑separated directories where the shell searches for executable programs; adding a directory makes commands in that location runnable without full paths.
# pwd
/root/docker/httpd
export PATH=$PATH:$PWD
echo $PATHHOME – user’s default working directory
echo $HOME
/home/rethinkHISTSIZE – number of commands stored in history (default 1000)
echo $HISTSIZE
1000
HISTSIZE=1001
echo $HISTSIZEHistory is kept in memory; use history to view, and !n to re‑execute command number n.
history 5
!62LOGNAME – current login name
echo $LOGNAME
rethinkHOSTNAME – system’s host name
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.
