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.
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 $PATHSetting 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 ~/.bashrcDeleting environment variables
Use the unset command. Example:
unset MY_VARIABLEExamples: Setting and using environment variables
Example 1: Temporary environment variable
export MY_VAR="temporary value"
echo $MY_VAROutput:
temporary valueExample 2: Permanently set PATH variable
Edit ~/.bashrc and add: export PATH=$PATH:/my/custom/path Save and apply:
source ~/.bashrcExample 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.shOutput:
My variable is: Hello from scriptSummary
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.
Signed-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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
