Fundamentals 6 min read

Master Linux Environment Variables: View, Set, and Delete Them

This guide explains what Linux environment variables are, lists common ones, and provides step‑by‑step commands for viewing, temporarily or permanently setting, and deleting variables, plus practical script examples to help users manage their shell environment efficiently.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Environment Variables: View, Set, and Delete Them

Environment Variables in Linux

What is an environment variable?

Environment variables are key‑value pairs maintained by the operating system. They are inherited by child processes and influence program behavior, such as search paths, user information, locale settings, and other configuration data.

Common environment variables

PATH : List of directories searched for executable files.

HOME : Current user's home directory.

USER : Logged‑in username.

SHELL : Path to the current shell program.

LANG : System language and character set.

PWD : Present working directory.

Viewing environment variables

Use any of the following commands in a terminal: printenv or env To display a specific variable, use echo $VARIABLE_NAME, for example:

echo $PATH

Setting environment variables

Temporary setting

Temporary variables exist only for the current shell session. Define them with export: export VARIABLE_NAME="value" Example:

export MY_VARIABLE="Hello, World!"

Permanent setting

To make a variable persist across sessions, add an export line to one of the shell initialization files. Typical files are: /etc/profile: System‑wide configuration. ~/.bashrc: Interactive Bash sessions for the current user. ~/.bash_profile: Login Bash sessions for the current user. ~/.profile: General user shell configuration.

Example – add a variable to ~/.bashrc: nano ~/.bashrc Append the line: export MY_VARIABLE="Hello, World!" Apply the changes without restarting the shell:

source ~/.bashrc

Deleting environment variables

Remove a variable from the current environment with unset:

unset MY_VARIABLE

Practical examples

Example 1 – Set a temporary variable

export MY_VAR="temporary value"
echo $MY_VAR

Output:

temporary value

Example 2 – Permanently add a directory to PATH

nano ~/.bashrc   # open the file for editing
# Append the following line at the end
export PATH=$PATH:/my/custom/path
source ~/.bashrc   # reload the file

Example 3 – Use an environment variable in a Bash script

#!/bin/bash
# myscript.sh

echo "My variable is: $MY_VAR"

Make the script executable and run it:

chmod +x myscript.sh
export MY_VAR="Hello from script"
./myscript.sh

Output:

My variable is: Hello from script
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 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.