How to Check, Set, and Delete Environment Variables in Ubuntu | Complete Guide

1. Introduction

In Linux distributions like Ubuntu, environment variables play a crucial role in configuring both the system and applications. They help customize and optimize system behavior and program execution. This article provides a step-by-step guide on how to check, set, and delete environment variables in Ubuntu, along with practical use cases. With clear command examples, even beginners can follow along, while system administrators and developers will find valuable insights.

2. What Are Environment Variables?

Environment variables are system-defined or user-defined variables that store specific configuration values, allowing programs and the system to operate dynamically based on those settings. They enable flexibility by modifying program behavior or changing system-wide settings.

Common Uses of Environment Variables

Environment variables are useful in various scenarios, such as:

  • PATH: Stores a list of directories where executable files are searched for. This allows you to run commands without specifying their full path.
  • HOME: Represents the user’s home directory, used to access user-specific configurations and files.
  • LANG: Manages system language settings, determining the language used by programs and system messages.

3. How to Check Environment Variables

Several commands are available to check environment variables. These commands help display currently set variables and retrieve the value of specific environment variables.

Checking a Specific Environment Variable

To check the value of a specific environment variable, use the echo command:

echo $PATH

This command displays the directories stored in the PATH variable, showing where the system searches for executable files.

Checking All Environment Variables

To list all environment variables currently set in the shell, use the env or printenv command:

env

This command outputs all environment variables along with their values. Additionally, the export -p command can be useful for viewing all exported environment variables.

4. How to Set Environment Variables

Setting Temporary Environment Variables

Temporary environment variables can be set using the export command. These variables exist only for the duration of the current shell session.

export MY_VARIABLE="hello"

This command assigns the value “hello” to the variable MY_VARIABLE. Once the shell session ends, the variable is cleared.

Setting Permanent Environment Variables

To make an environment variable persist across sessions, add it to the .bashrc or .profile file. This ensures the variable remains set even after restarting the system.

  1. Adding to .bashrc:
echo 'export MY_VARIABLE="hello"' >> ~/.bashrc
  1. Adding to .profile:
echo 'export MY_VARIABLE="hello"' >> ~/.profile

To apply the changes immediately, reload the file using:

source ~/.bashrc

This ensures that the variable is set every time a new shell session starts.

5. How to Delete Environment Variables

Deleting Temporary Environment Variables

To delete an environment variable in the current shell session, use the unset command:

unset MY_VARIABLE

This removes the variable from the current session, but it will not affect system-wide settings or persistent configurations.

Deleting Permanent Environment Variables

If the variable was added to .bashrc or .profile, remove the corresponding line from the file. You can use a text editor such as nano or vim to edit the file:

nano ~/.bashrc

After removing the line, save the file and reload the settings:

source ~/.bashrc

6. Troubleshooting Common Issues

When working with environment variables in Ubuntu, you might encounter certain issues. Here are some common problems and how to resolve them.

Environment Variable Not Persisting After Reboot

If an environment variable disappears after restarting the system, check whether it was added to the correct file:

  • .bashrc: Used for interactive shell sessions (recommended for user-defined variables).
  • .profile: Loaded at login and useful for defining variables for all shells.

Changes Not Taking Effect

If a newly set environment variable does not take effect, try the following:

  • Ensure you have run source ~/.bashrc or source ~/.profile after making changes.
  • Check if another script or profile file is overriding the variable.
  • Restart the terminal or log out and log back in.

7. Conclusion

Understanding environment variables is essential for managing Ubuntu systems efficiently. This guide has covered:

  • How to check environment variables using echo, env, and printenv.
  • How to set temporary and permanent environment variables.
  • How to delete variables both temporarily and permanently.
  • Common issues and troubleshooting steps.

By mastering these concepts, you can improve system management, automate processes, and customize your development environment more effectively. Whether you are a beginner or an advanced user, environment variables are a fundamental tool for working with Ubuntu.