environment-variables linux unset

How do I delete an exported environment variable?


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?



unset is the command you’re looking for.



  • 8

    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, 2014 at 15:35

  • 48

    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 <X> ~” where <X> is the name of the variable. This may take a while if you have a lot of files in your home directory.

    – matt5784

    May 8, 2014 at 1:11

  • 3

    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 ?

    Dec 10, 2016 at 22:05

  • 5

    @olejorgenb At least in bash, you can say declare +x MYVAR to remove the export but keep the value in the current shell.

    Dec 15, 2016 at 15:09

  • 15

    @PederKlingenberg export -n MYWAR works as well in Bash.

    – jarno

    May 3, 2017 at 12:19


Walkthrough of creating and deleting an environment variable in Bash:

Test if the DUALCASE variable exists (empty output):

env | grep DUALCASE

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


Check if it is there:

env | grep DUALCASE



It is there. So get rid of it:


Check if it’s still there (empty output):

env | grep DUALCASE

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:

CAN="chuck norris"
set | grep CAN


CAN=’chuck norris’

env | grep CAN # Empty output

exec bash
set | grep CAN
env | grep CAN # Empty output

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

Unset all environment variables back to default on login:

export DOGE="so wow"
env | grep DOGE


DOGE=so wow

env -i bash
env | grep DOGE # Empty output

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


  • 13

    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 <Tab> (if it exists, that also may be a hint to what you wanna do).

    Jan 12, 2016 at 17:44

  • 13

    ‘env | grep VARIABLE’ is better than ‘echo $VARIABLE’ because I can tell it’s truly gone

    – calasyr

    Sep 23, 2016 at 18:16

  • 10

    @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

    Oct 4, 2016 at 8:07

  • 4

    Note that env -i bash seems to be creating a subshell (at least on a Mac) which may have unintended consequences.

    Jul 28, 2017 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='.

    Feb 1, 2019 at 14:22


The original question doesn’t mention how the variable was set, but:

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 the setenv command are automatically exported to a subshell while variables 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’.



  • 1

    Great addition about the differences between set / setenv wrt subshells!

    – 4levels

    Mar 5, 2019 at 14:40