How to List, Set and Manage Linux Environment Variables

March 4, 2025 / General Discussion

Linux environment variables play a crucial role in configuring the operating system and the applications running on it. They store information about the system environment, such as the user’s home directory, the current shell, and the path to executable files.

By understanding how to list, set, and manage these variables, you can optimize your workflow, automate tasks, and ensure the smooth operation of software. This guide will walk you through the essential commands and techniques to effectively handle environment variables in Linux.

Linux Environment Variables

1. Listing Environment Variables

To view all environment variables, you can use the following commands:

  • printenv: Displays all environment variables.
    printenv
  • env: Another command to display all environment variables.
    env
  • set: Shows all shell variables, including environment variables.
    set
  • echo: To display the value of a specific variable.
    echo $VARIABLE_NAME

2. Setting Environment Variables

You can set environment variables using the following methods:

  • Temporarily (for the current session):
    export VARIABLE_NAME=value
  • Permanently (by adding to configuration files):
    • For Bash Shell: Add to ~/.bashrc or ~/.bash_profile.
    • For Zsh Shell: Add to ~/.zshrc.
    • For Sh and Ksh: Add to ~/.profile.Example:
      echo ‘export VARIABLE_NAME=value’ >> ~/.bashrc

3. Unsetting Environment Variables

To remove or unset an environment variable:

  • Temporarily:
    unset VARIABLE_NAME
  • Permanently: Remove the corresponding line from the configuration file
    (~/.bashrc, ~/.bash_profile, etc.).

4. Managing Environment Variables

Viewing a Specific Variable

To view the value of a specific environment variable, use:
echo $VARIABLE_NAME

Modifying a Variable

To change the value of an existing environment variable:
export VARIABLE_NAME=new_value

Persisting Changes

  • After updating configuration files, make sure to apply the changes using:
    source ~/.bashrc

Additional Tips

  • Using env Command in Scripts: To set an environment variable for a single command execution, use:
    env VARIABLE_NAME=value command

Checking if a Variable Exists:
if [ -z ${VARIABLE_NAME+x} ]; then echo “VARIABLE_NAME is unset”; else echo “VARIABLE_NAME is set to ‘$VARIABLE_NAME'”; fi

  • Using printenv in Scripts: To check environment variables within a script:
    if [ “$(printenv VARIABLE_NAME)” ]; then
    echo “VARIABLE_NAME is set”
    else
    echo “VARIABLE_NAME is not set”
    fi

Effectively managing Linux environment variables is key to optimizing your system’s performance and personalizing your workflow. By mastering these skills, you can ensure your Linux environment runs smoothly and efficiently, tailored to your specific needs.

Learn More: How to Install Linux Windows Subsystem on Windows 11: Easy Guide

Leave a Reply

Your email address will not be published. Required fields are marked *