Master Linux Environment Variables: Permanent and Temporary Settings
This guide explains what Linux environment variables are, how to set permanent variables for all users or a single user via /etc/profile, .bash_profile or .bashrc, how to create temporary variables for the current shell, and lists common commands and typical variables such as PATH, HOME, HISTSIZE, LOGNAME, HOSTNAME, and SHELL.
What are environment variables?
Linux is a multi‑user operating system; each logged‑in user gets a personal environment defined by a set of variables.
Setting permanent variables for all users
Modify the system‑wide file /etc/profile as root. Example:
# vi /etc/profile
export CLASSPATH=./JAVA_HOME/lib:$JAVA_HOME/jre/libAfter editing, run source /etc/profile to apply the changes immediately; otherwise they take effect on the next login.
Setting permanent variables for a single user
Edit the hidden file ~/.bash_profile (or ~/.bashrc) in the user’s home directory. Example:
# vi /home/rethink/.bash_profile
export CLASSPATH=./JAVA_HOME/lib:$JAVA_HOME/jre/lib
source /home/rethink/.bash_profile .bash_profileis read by login shells, while .bashrc is read by non‑login interactive shells; both can be used to set user‑specific variables.
Temporary variables (current shell only)
Define them directly in the terminal; they disappear when the shell exits.
export NAME="rethink"
echo $NAMECommon commands for environment variables
echo $NAME– display the value of a variable. export NAME='value' – create or modify a variable. env – list all environment variables. set – list shell variables (including user variables). unset NAME – delete a variable. readonly NAME – make a variable read‑only (cannot be unset).
Typical environment variables
PATH – colon‑separated list of directories searched for executable programs. export PATH=$PATH:$PWD HOME – the user’s home directory. echo $HOME HISTSIZE – number of commands stored in the command history (default 1000).
echo $HISTSIZE
HISTSIZE=1001
echo $HISTSIZELOGNAME – the login name of the current user. echo $LOGNAME HOSTNAME – the machine’s network name. echo $HOSTNAME SHELL – the path to the current shell executable.
echo $SHELLSigned-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.
