How do I delete or remove an exported environment variable?


👤 Diwas Poudel    🕒 12 Sep 2023    📁 FIX

Environment variables are useful OS features that allow programs and operating systems to know where files are installed, where to store temporary files, where to find user profile settings, and many more.

However, environmental variables can cause issues for you in some instances of your life. One of the issues or problems looks like this: Sometimes it happens that you already have exported to set the environment variables for the program or driver but you haven't installed the software yet. Now if you try to install the same software you get some error.

What is the exported Environment variable?

Exported environment variables are environment variables that are made available to the child process which means that when a process creates a child process then the child process will inherit values of all parent process's exported environment variables.

To export the env variable we use the following command:

export <variable_name>=<value>

So, one solution to this problem is to remove previously set user variables from Environment Variables,  or we can say unexported environment variables.

Method 1: Using an unset command

The UnSet Command is useful for removing variables from the environment. It completely removes the variables from the list of variables that it tracks. When you unset a variable, it is no longer accessible because it has been removed from the environment variable list.

Also, note that with an unset command, you cannot unset read-only marked variables.

Syntax:

UNSET variable_name 

If you want to unset for variable: GNUPLOT_DRIVER_DIR from the environment variable then you can do this:

UNSET GNUPLOT_DRIVER_DIR

If you used to export to set the variable, unset GNUPLOT DRIVER DIR should work fine.

Also, if you have it permanently set in ~/.bashrc or ~/.zshrc, you must remove it from these files to make it work again.

Also Read: Reason Why Windows OS is popular than Other OS?

For Example:

If you export variable name="setting.txt" setting, you can view it using the GREP command inside env, and then unset that env. variables, and then when you look at the same variable again, you will find that env. variable "variable_name" is already removed.

  • The unset command will only delete the environment variable from the current shell. If you want to delete the environment variable from all shells, you will need to edit your shell's configuration file.
$ export variable_name="setting.txt"
$ env | grep variable_name
variable_name=setting.txt
$ unset variable_name
$ env | grep variable_name

You can also unset multiple variable like this:

unset First_Variable Second_Variable

Note that the unset command will not delete the value of the environment variable. For this, we need to use unsetenv variable as shown below.

unsetenv MY_ENV_VAR

For multiple variables, we can do the following.

unsetenv MY_ENV_VAR MY_SECOND_ENV_VAR

Method 2: Using the env command 

The env command has a unique feature that allows you to remove or delete environment variables.

Syntax:

env -u NAME

For the GNUPLOT_DRIVER_DIR variable, you can simply do this:

env -u GNUPLOT_DRIVER_DIR

it will remove the variable GNUPLOT_DRIVER_DIR from the environment.

Also Read: The Secret Of CPU HYPERTHREADING In Depth

Method 3: Set Empty Value for Variable

Also, if the above method does not work, try assigning an empty value to the variables as follows:

If the variable exists in the env variable, set it to the empty string as follows: export variable name = This will set the variable "variable name" to empty. Simply replace the variable name with your variable's name, such as GNUPLOT DRIVER DIR.

$ export variable_name="setting.txt"
$ env | grep variable_name
variable_name=setting.txt
$ export variable_name=
$ env | grep variable_name
variable_name=

Conclusion

In this guide, we have shown you how to remove or delete the environment variables.