Fundamentals 6 min read

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.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Environment Variables: Permanent and Temporary Settings

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/lib

After 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_profile

is 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 $NAME

Common 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 $HISTSIZE

LOGNAME – 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 $SHELL
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

sysadmincommand-lineenvironment-variables
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.