Advertisement
Advertisement


How do I delete an exported environment variable?


Question

Before installing gnuplot, I set the environment variable GNUPLOT_DRIVER_DIR = /home/gnuplot/build/src. During the installation, something went wrong.

I want to remove the GNUPLOT_DRIVER_DIR environment variable. How can I achieve it?

2017/10/30
1
1611
10/30/2017 4:48:11 AM

Accepted Answer

unset is the command you're looking for.

unset GNUPLOT_DRIVER_DIR
2011/07/29
2563
7/29/2011 7:00:39 PM

Walkthrough of creating and deleting an environment variable in bash:

Test if the DUALCASE variable exists:

[email protected]:~$ env | grep DUALCASE
[email protected]:~$ 

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

[email protected]:~$ DUALCASE=1
[email protected]:~$ export DUALCASE

Check if it is there:

[email protected]:~$ env | grep DUALCASE
DUALCASE=1

It is there. So get rid of it:

[email protected]:~$ unset DUALCASE

Check if it's still there:

[email protected]:~$ env | grep DUALCASE
[email protected]:~$ 

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:

[email protected]:~$ CAN="chuck norris"
[email protected]:~$ set | grep CAN
CAN='chuck norris'
[email protected]:~$ env | grep CAN
[email protected]:~$
[email protected]:~$ exec bash
[email protected]:~$ set | grep CAN
[email protected]:~$ env | grep CAN
[email protected]:~$

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

Unset all environment variables back to default on login:

[email protected]:~$ export DOGE="so wow"
[email protected]:~$ env | grep DOGE
DOGE=so wow
[email protected]:~$ env -i bash
[email protected]:~$ env | grep DOGE
[email protected]:~$

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

2014/09/22

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

2019/01/08

this may also work.

export GNUPLOT_DRIVER_DIR=
2017/12/20

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.

2020/02/13

Source: https://stackoverflow.com/questions/6877727
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]