How do I delete or remove an exported environment variable?


๐Ÿ‘คย Diwas Poudel ย ย  ๐Ÿ•’ 24 Apr 2022 ย ย  ๐Ÿ“ 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, Environment 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.

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 readonly 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 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.

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

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 like 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.