Operations 6 min read

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.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Linux Environment Variables: Set, Update, and Use Them Effectively
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/lib
After 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 $NAME

Common 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/bin
PATH 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 $PATH

HOME – user’s default working directory

echo $HOME
/home/rethink

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

echo $HISTSIZE
1000
HISTSIZE=1001
echo $HISTSIZE
History is kept in memory; use history to view, and !n to re‑execute command number n.
history 5
!62

LOGNAME – current login name

echo $LOGNAME
rethink

HOSTNAME – system’s host name

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.

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