Fundamentals 7 min read

Master Linux Environment Variables: Permanent and Temporary Settings Explained

This guide explains how Linux environment variables work, covering system‑wide and per‑user permanent configurations, temporary session variables, common variables like PATH and HOME, and essential commands for viewing, setting, and managing them.

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

What Are Environment Variables?

Linux is a multi‑user operating system where each user has a dedicated environment defined by a set of variables called environment variables. Users can modify these variables to tailor their shell behavior.

How to Set Environment Variables

Permanent Variables for All Users

To make a variable available to every user, edit /etc/profile (only root can modify this file) and add an export line, for example:

# vi /etc/profile
export CLASSPATH=./JAVA_HOME/lib:$JAVA_HOME/jre/lib

After saving, run source /etc/profile to apply the change immediately; otherwise it takes effect at the next login.

Permanent Variables for a Single User

For a specific user, add the export line to ~/.bash_profile (or ~/.bashrc). Example:

# vi /home/rethink/.bash_profile
export CLASSPATH=./JAVA_HOME/lib:$JAVA_HOME/jre/lib
source /home/rethink/.bash_profile

The two files differ: .bash_profile is read once at a login shell start, while .bashrc is read for each interactive non‑login shell.

Temporary Variables (Current Shell Only)

Use the export command directly in the terminal; the variable disappears when the shell exits.

export NAME='rethink'
echo $NAME

Common Environment Variables

PATH – Colon‑separated list of directories searched for executables.

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

HOME – User's home directory.

echo $HOME
/home/rethink

HISTSIZE – Number of commands stored in history (default 1000).

echo $HISTSIZE
1000
HISTSIZE=1001
echo $HISTSIZE
1001

LOGNAME – Current login name.

echo $LOGNAME
rethink

HOSTNAME – System host name.

echo $HOSTNAME
JDu4e00u53f7

SHELL – Path to the user's default shell.

echo $SHELL
/bin/bash

Useful Commands for Managing Variables

echo

– Print the value of a variable, e.g., echo $NAME. export – Define a new environment variable, e.g., export NAME='rethink'. env – List all environment variables for the current user. set – Show shell variables, including user‑defined ones. unset – Remove a variable, e.g., unset NAME. readonly – Mark a variable as read‑only; it cannot be unset. history – Display command history; use !n to re‑execute command number n.

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.

LinuxShellcommand-lineSystem AdministrationBashEnvironment 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.