Managing Linux Environment Variables

Environment variables play a crucial role in Linux systems by storing information that can be accessed by various processes and applications.
Properly managing these variables helps customize your working environment and ensure applications function correctly.
This guide covers everything you need to know about viewing, setting, and removing environment variables in Linux.

What Are Environment Variables?

Environment variables are dynamic values that affect how processes run on your system. They’re part of the environment in which a process runs and typically consist of a name-value pair like NAME=value.

Viewing Environment Variables

View All Environment Variables

To display all currently set environment variables, use either:

1
env

or

1
printenv

View a Specific Environment Variable

To check the value of a particular variable:

1
echo $VARIABLE_NAME

For example:

1
echo $PATH

Setting Environment Variables

Temporary: For Current Session Only

To set a variable for the current shell session only:

1
VARIABLE_NAME=value

To make this variable available to child processes (export it):

1
export VARIABLE_NAME=value

For example:

1
export DEBUG_MODE=true

Permanent: User-Specific Settings

To make variables persist across sessions for a specific user, add them to shell configuration files:

  1. For Bash users, edit ~/.bashrc or ~/.bash_profile:
1
echo 'export VARIABLE_NAME=value' >> ~/.bashrc
  1. For Zsh users, edit ~/.zshrc:
1
echo 'export VARIABLE_NAME=value' >> ~/.zshrc
  1. Load the changes without restarting your session:
1
source ~/.bashrc  # For Bash

Permanent: System-Wide Settings

To set variables for all users:

  1. Create a file in /etc/profile.d/:
1
sudo nano /etc/profile.d/custom-environment.sh
  1. Add your exports:
1
export VARIABLE_NAME=value
  1. Make it executable:
1
sudo chmod +x /etc/profile.d/custom-environment.sh

Another system-wide option is to add entries to /etc/environment:

1
sudo nano /etc/environment

Add variables in the format:

1
VARIABLE_NAME=value

Note: /etc/environment doesn’t support shell functions or commands - only simple variable assignments.

Removing Environment Variables

For Current Session

To unset a variable in the current session:

1
unset VARIABLE_NAME

Permanently

To permanently remove a variable, edit the file where it was defined and remove or comment out the corresponding line, then reload the configuration file.

Appending to Existing Variables

To add values to existing variables like PATH:

1
export PATH=$PATH:/new/directory

This preserves the original value while adding new content.

Common Environment Variables

  • PATH: Directories searched for executable files
  • HOME: Current user’s home directory
  • USER: Current username
  • SHELL: Path to current shell
  • LANG: System language and locale
  • PWD: Current working directory
  • TERM: Terminal type

Best Practices

  1. Use descriptive variable names (preferably uppercase)
  2. Avoid spaces in values without quotes
  3. Organize environment variables by purpose
  4. Document non-standard variables
  5. Be careful with system variables like PATH - always append rather than overwrite

Managing Linux Environment Variables
https://www.hardyhu.cn/2025/03/20/Managing-Linux-Environment-Variables/
Author
John Doe
Posted on
March 20, 2025
Licensed under