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.
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 $PATHSetting 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 ~/.bashrcDeleting environment variables
Remove a variable from the current environment with unset:
unset MY_VARIABLEPractical examples
Example 1 – Set a temporary variable
export MY_VAR="temporary value"
echo $MY_VAROutput:
temporary valueExample 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 fileExample 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.shOutput:
My variable is: Hello from scriptSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
