linux - How do I delete an exported environment variable?

ID : 477

viewed : 231

Tags : linuxenvironment-variablesunsetlinux





Top 5 Answer for linux - How do I delete an exported environment variable?

vote vote

97

unset is the command you're looking for.

unset GNUPLOT_DRIVER_DIR 
vote vote

85

Walkthrough of creating and deleting an environment variable in bash:

Test if the DUALCASE variable exists:

el@apollo:~$ env | grep DUALCASE el@apollo:~$  

It does not, so create the variable and export it:

el@apollo:~$ DUALCASE=1 el@apollo:~$ export DUALCASE 

Check if it is there:

el@apollo:~$ env | grep DUALCASE DUALCASE=1 

It is there. So get rid of it:

el@apollo:~$ unset DUALCASE 

Check if it's still there:

el@apollo:~$ env | grep DUALCASE el@apollo:~$  

The DUALCASE exported environment variable is deleted.

Extra commands to help clear your local and environment variables:

Unset all local variables back to default on login:

el@apollo:~$ CAN="chuck norris" el@apollo:~$ set | grep CAN CAN='chuck norris' el@apollo:~$ env | grep CAN el@apollo:~$ el@apollo:~$ exec bash el@apollo:~$ set | grep CAN el@apollo:~$ env | grep CAN el@apollo:~$ 

exec bash command cleared all the local variables but not environment variables.

Unset all environment variables back to default on login:

el@apollo:~$ export DOGE="so wow" el@apollo:~$ env | grep DOGE DOGE=so wow el@apollo:~$ env -i bash el@apollo:~$ env | grep DOGE el@apollo:~$ 

env -i bash command cleared all the environment variables to default on login.

vote vote

72

Because the original question doesn't mention how the variable was set, and because I got to this page looking for this specific answer, I'm adding the following:

In C shell (csh/tcsh) there are two ways to set an environment variable:

  1. set x = "something"
  2. setenv x "something"

The difference in the behaviour is that variables set with setenv command are automatically exported to subshell while variable set with set aren't.

To unset a variable set with set, use

unset x 

To unset a variable set with setenv, use

unsetenv x 

Note: in all the above, I assume that the variable name is 'x'.

credits:

https://www.cyberciti.biz/faq/unix-linux-difference-between-set-and-setenv-c-shell-variable/ https://www.oreilly.com/library/view/solaristm-7-reference/0130200484/0130200484_ch18lev1sec24.html

vote vote

69

As mentioned in the above answers, unset GNUPLOT_DRIVER_DIR should work if you have used export to set the variable. If you have set it permanently in ~/.bashrc or ~/.zshrc then simply removing it from there will work.

vote vote

54

this may also work.

export GNUPLOT_DRIVER_DIR= 

Top 3 video Explaining linux - How do I delete an exported environment variable?







Related QUESTION?