Operations 6 min read

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.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Linux Environment Variables: How to Set, Update, and Use Them

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

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

The 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
rethink

Common Environment Variables

PATH
# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

The 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/httpd

HOME – the user’s home directory

$ whoami
rethink
echo $HOME
/home/rethink

HISTSIZE – number of commands saved in history (default 1000)

echo $HISTSIZE
1000
HISTSIZE=1001
echo $HISTSIZE
1001

History 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
rethink

HOSTNAME – host name of the machine

echo $HOSTNAME
JDu4e00u53f7

SHELL – path to the current shell

echo $SHELL
/bin/bash
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.

LinuxShellSystem AdministrationEnvironment Variables
MaGe Linux Operations
Written by

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.

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.