How to Change Environment Variable in Linux for a User or Globally

Shuaeb Mohammad
3 min readJan 23, 2025

--

Linux environment variables are an integral part of the operating system, enabling users and applications to interact efficiently with the underlying environment. By understanding how to modify these variables, you can customize the behavior of your system, configure development environments, and optimize workflows. This article will provide a comprehensive guide to viewing, adding, modifying, and exporting environment variables in Linux, both for a single user and globally.

Understanding Environment Variables in Linux

Environment variables are dynamic values that affect the way processes behave on a Linux system. They serve as a communication channel between the system and applications.

Key Terms:

  • Environment Variables: Dynamic key-value pairs accessible to processes and applications. Examples include $PATH, $HOME, and $LANG
  • User-Specific Variables: Variables specific to a single user’s environment.
  • Global Variables: System-wide variables accessible by all users.

Common Variables:

  • $PATH: Specifies directories the shell searches for executable files.
  • $HOME: Points to the user’s home directory.
  • $LANG: Defines the system’s language and locale settings.

Temporary vs. Permanent Variables:

  • Temporary Variables: Exist only for the current session.
  • Permanent Variables: Persist across sessions and reboots.

How to View Current Environment Variables

List All Variables:

env
printenv

Check a Specific Variable:

echo $VARIABLE_NAME

Example:

echo $PATH

Setting Environment Variables for a Single User

Temporary Changes

To set a variable temporarily for the current session:

export VAR_NAME=value

Example:

export MY_VAR="Hello World"
echo $MY_VAR

Permanent Changes

To make the variable persistent, add it to shell configuration files.

Step-by-Step:

  1. Open the configuration file using a text editor. Common files include:
  • ~/.bashrc (for Bash)
  • ~/.zshrc (for Zsh)
  • ~/.bash_profile (for login shells)
nano ~/.bashrc

2. Add the variable at the end of the file:

export VAR_NAME=value

3. Save and exit (Ctrl+O, Enter, Ctrl+X in nano).

4. Reload the file:

source ~/.bashrc

Setting Global Environment Variables

Global variables apply to all users and are typically defined in system-wide configuration files.

Temporary Changes

Set a global variable for the current session using:

sudo export VAR_NAME=value

Permanent Changes

Step-by-Step:

  1. Open a system-wide configuration file:
  • /etc/environment: Best for simple key-value pairs.
  • /etc/profile or /etc/profile.d/*.sh: Used for scripts and complex configurations.
sudo nano /etc/environment

2. Add the variable:

VAR_NAME=value

3. Save and exit.

4. Reload the file or restart the system:

source /etc/environment

Practical Examples of Environment Variable Modifications

Example 1: Adding a Directory to the $PATH Variable

export PATH=$PATH:/new/directory/path

To make this change permanent, add it to ~/.bashrc or /etc/environment.

Example 2: Setting the JAVA_HOME Variable

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH

Example 3: Configuring Proxy Settings

export http_proxy=http://proxy.example.com:8080
export https_proxy=https://proxy.example.com:8080

Best Practices for Managing Environment Variables

  • Use meaningful and consistent names for custom variables.
  • Avoid storing sensitive data like API keys in global variables.
  • Backup configuration files before making changes.
  • Test changes in a temporary session before applying them permanently.

Troubleshooting Tips

  • Changes Not Taking Effect: Ensure you’ve reloaded the configuration file using source or restarted the shell.
  • Syntax Errors: Double-check for typos or missing = signs.
  • Permissions Issues: Use sudo when editing system-wide files.

Debugging Tools:

  • Use env or grep to verify variable values:
env | grep VAR_NAME

Linux environment variables are powerful tools for customizing and managing your system. By mastering the steps outlined in this guide, you can confidently modify user-specific and global variables to suit your needs. Experiment with the examples provided and explore additional customizations to enhance your productivity.

If you found this guide helpful, share your experiences or questions in the comments. Happy configuring!

--

--

Shuaeb Mohammad
Shuaeb Mohammad

Written by Shuaeb Mohammad

An experienced professional with a broad skill set that encompasses the fields of programming, system analysis, software engineering, and information technology

No responses yet