1797

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?

kiamlaluno
  • 24,790
  • 16
  • 70
  • 85
A. K.
  • 26,873
  • 15
  • 44
  • 74
  • 2
    For those looking for how to do this in Fish shell see https://stackoverflow.com/questions/30703860/how-do-i-unset-a-variable-in-the-fish-shell (even though this question isn't for a specific shell) – Elijah Lynn Apr 04 '19 at 03:00

5 Answers5

2836

unset is the command you're looking for.

unset GNUPLOT_DRIVER_DIR
Peder Klingenberg
  • 30,169
  • 1
  • 15
  • 23
  • 6
    but this only works for a session, what about unsetting it definitely? or maybe searching where is the variable set, so you can go and delete it? – eLRuLL Apr 19 '14 at 15:35
  • 39
    This should work per terminal instance. Generally, each time a terminal window is opened, it will load up variables from various places such as ~/.bashrc, ~/.profile, etc. Any variables you set in one terminal instance will not carry over to another. If you have a variable which seems to be set automatically every time you open terminal, try looking through the various hidden files in your home directory for it. Or, to see where it is being set, try "grep -r ~" where is the name of the variable. This may take a while if you have a lot of files in your home directory. – matt5784 May 08 '14 at 01:11
  • 2
    This removes the variable from the shell too though. Is the only way to `unexport` to do `T="$MYVAR"; unset MYVAR; MYVAR="$T"; unset T` ? – olejorgenb Dec 10 '16 at 22:05
  • 4
    @olejorgenb At least in bash, you can say `declare +x MYVAR` to remove the export but keep the value in the current shell. – Peder Klingenberg Dec 15 '16 at 15:09
  • 13
    @PederKlingenberg `export -n MYWAR` works as well in Bash. – jarno May 03 '17 at 12:19
191

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.

Eric Leschinski
  • 123,728
  • 82
  • 382
  • 321
  • 11
    maybe `echo $VARIABLE` is better than `env | grep VARIABLE`, it's lighter as it doesn't need to print all variables and then send its output to another (grep) process. Plus, `env | VARIABLE` could catch more than one variable that matches the same pattern. Plus2, `echo $VARIABLE` makes possible to complete variable's name by hitting (if it exists, that also may be a hint to what you wanna do). – Rodrigo Gurgel Jan 12 '16 at 17:44
  • 11
    'env | grep VARIABLE' is better than 'echo $VARIABLE' because I can tell it's truly gone – calasyr Sep 23 '16 at 18:16
  • 8
    @RodrigoGurgel, `echo $VARIABLE` doesn't tell you whether the VARIABLE is a shell variable (here called "local variable") or an environment variable, which is the whole point of the walkthrough. – hmijail mourns resignees Oct 04 '16 at 08:07
  • 4
    Note that `env -i bash` seems to be creating a subshell (at least on a Mac) which may have unintended consequences. – Mark Chackerian Jul 28 '17 at 13:46
  • 3
    @RodrigoGurgel using echo won't show existing variable set to empty string or nul. to your point, though, a proper way to test for variable would be `env | grep -e '^VARNAME='`. – Thomas Feb 01 '19 at 14:22
8

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

Randall
  • 2,454
  • 1
  • 17
  • 21
G Eitan
  • 81
  • 1
  • 3
6

this may also work.

export GNUPLOT_DRIVER_DIR=
Nilesh K.
  • 155
  • 1
  • 3
  • 12
    The variable still exists, but it contains an empty string, as you can see in the output of the `env` command. It just might be the case that the application that uses the variable does not distinguish between non-existent and empty environment variable. – Palec Dec 24 '17 at 15:40
  • 1
    yes it will contain, this was just to remove value not to remove variable. But yes one can use - unset GNUPLOT_DRIVER_DIR. – Nilesh K. Jan 02 '18 at 06:18
  • This doesn't work in the case of the PAGER variable. I tried to unset my PAGER setting with `export PAGER=`, but that just disabled paging entirely--all my man pages just dumped straight to the terminal. `unset PAGER` did the trick, reverting it to default behaviour. – Chad Dec 13 '19 at 22:06
3

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.

Rishabh Bohra
  • 80
  • 1
  • 7