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 to view, temporarily or permanently set, and delete them, plus practical examples for using variables in scripts, helping users efficiently manage their system configuration.

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

What is an environment variable?

Environment variables are variables used by the operating system to store information about the runtime environment, providing a way to share configuration data between programs. They affect process behavior and their child processes and play an important role in the OS.

Common environment variables

PATH : Specifies the search path for executable files.

HOME : The current user's home directory.

USER : The username of the currently logged‑in user.

SHELL : The type of shell currently in use.

LANG : System language and character set settings.

PWD : The current working directory.

Viewing environment variables

In Linux you can use the following commands to view the current environment variables: printenv or env To view the value of a specific variable, use the echo command, for example:

echo $PATH

Setting environment variables

Temporary setting

Temporary environment variables are only valid for the current terminal session. Set them with: export VARIABLE_NAME="value" Example:

export MY_VARIABLE="Hello, World!"

Permanent setting

To make environment variables persist across all sessions, add them to configuration files. Common files include:

/etc/profile : System‑wide environment variable settings.

~/.bashrc : Current user's Bash shell configuration file.

~/.bash_profile : Current user's login shell configuration file.

~/.profile : General configuration file for the current user's shell.

Example: edit ~/.bashrc and add the following line: export MY_VARIABLE="Hello, World!" Save and close the file, then apply the changes:

source ~/.bashrc

Deleting environment variables

Use the unset command. Example:

unset MY_VARIABLE

Examples: Setting and using environment variables

Example 1: Temporary environment variable

export MY_VAR="temporary value"
echo $MY_VAR

Output:

temporary value

Example 2: Permanently set PATH variable

Edit ~/.bashrc and add: export PATH=$PATH:/my/custom/path Save and apply:

source ~/.bashrc

Example 3: Use environment variable in a script

Create a simple Bash script, e.g., myscript.sh:

#!/bin/bash
echo "My variable is: $MY_VAR"

Make the script executable: chmod +x myscript.sh Set the variable and run the script:

export MY_VAR="Hello from script"
./myscript.sh

Output:

My variable is: Hello from script

Summary

Environment variables are crucial in Linux, providing a flexible way to configure the system and programs. By learning how to view, set, and delete them, users can better manage and optimize their Linux environment.

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.

System AdministrationbashEnvironment Variables
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.