Skip to content
Master Linux Environment Variables: Permanent, User, and Temporary Settings Explained
Operations 7 min read

Master Linux Environment Variables: Permanent, User, and Temporary Settings Explained

Learn how to define, modify, and apply Linux environment variables permanently for all users or individually, as well as temporary session variables, with clear examples of editing /etc/profile, .bash_profile, .bashrc, and using export, echo, and other common commands.

Liangxu Linux
Liangxu Linux
Liangxu Linux

What Are Linux Environment Variables?

Linux is a multi‑user operating system, and each user has a personal runtime environment defined by a set of environment variables. Modifying these variables lets you tailor the environment to your needs.

Permanent Variables for All Users

Variables placed in /etc/profile apply system‑wide, affecting every user. Only the root account can edit this file.

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

After editing, run source /etc/profile to apply changes immediately; otherwise they take effect at the next login.

Permanent Variables for a Single User

For user‑specific settings, add export lines to ~/.bash_profile (login shells) or ~/.bashrc (non‑login shells). Both files are hidden; list them with ls -a.

# 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 login, while .bashrc is read for each new terminal session.

Temporary Session Variables

Variables defined directly in the shell with export VAR=value exist only for the current session. They disappear when the terminal is closed or the user logs out.

$ export NAME="rethink"
$ echo $NAME
rethink

Common Commands for Managing Variables

echo – prints the value of a variable, e.g., echo $NAME .

export – creates or updates a variable, e.g., export NAME='rethink' .

env – lists all environment variables for the current user.

set – shows all shell variables, including user‑defined ones.

unset – removes a variable, e.g., unset NAME .

readonly – makes a variable immutable; unset will no longer affect it.

Frequently Used Environment Variables

PATH – colon‑separated list of directories searched for executable files. Example:

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

To prepend the current directory:

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

$ echo $HOME
/home/rethink

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

$ echo $HISTSIZE
1000
$ HISTSIZE=1001
$ echo $HISTSIZE
1001

LOGNAME – the login name of the current user.

$ echo $LOGNAME
rethink

HOSTNAME – the system's host name.

$ echo $HOSTNAME
JDu4e00u53f7

SHELL – path to the user's default 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.

Linuxsystem-administrationenvironment variables
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.