2285

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
A. K.
  • 34,395
  • 15
  • 52
  • 89
  • 5
    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

7 Answers7

3583

unset is the command you're looking for.

unset GNUPLOT_DRIVER_DIR
Peder Klingenberg
  • 37,937
  • 1
  • 20
  • 23
  • 12
    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
  • 59
    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
  • 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` ? – olejorgenb Dec 10 '16 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. – Peder Klingenberg Dec 15 '16 at 15:09
  • 15
    @PederKlingenberg `export -n MYWAR` works as well in Bash. – jarno May 03 '17 at 12:19
  • What's the command to unset it across different terminal sessions? – default123 Jul 18 '23 at 19:47
  • @default123 There is no single command to unset it across existing sessions. It will have to be unset in each session. You are probably better off finding out which initialization file sets it in the first place and edit that, which will remove it from future sessions. See other comments and answers for details of that. In my experience, calling `unset` is a very rare thing to need to do. – Peder Klingenberg Jul 19 '23 at 07:02
242

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:

DUALCASE=1
export DUALCASE

Check if it is there:

env | grep DUALCASE

Output:

DUALCASE=1

It is there. So get rid of it:

unset DUALCASE

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

Output:

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

Output:

DOGE=so wow

env -i bash
env | grep DOGE # Empty output

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • 16
    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
  • 14
    'env | grep VARIABLE' is better than 'echo $VARIABLE' because I can tell it's truly gone – calasyr Sep 23 '16 at 18:16
  • 12
    @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 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 BDX Feb 01 '19 at 14:22
30

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

Credits:

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
G Eitan
  • 410
  • 4
  • 5
17

This may also work.

export GNUPLOT_DRIVER_DIR=
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nilesh K.
  • 371
  • 3
  • 4
  • 21
    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
  • 1
    Perhaps it is time to [update](https://stackoverflow.com/posts/47904504/edit) your answer? (But ***without*** "Edit:", "Update:", or similar - the question/answer should appear as if it was written today.) – Peter Mortensen Jan 26 '22 at 00:01
12

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.

borz
  • 313
  • 4
  • 10
7

On Linux and macOS, you can use the command unset to remove an environment variable

unset GNUPLOT_DRIVER_DIR

Remove the variable permanently,

In Linux

You can edit your shell profile file, such as .bashrc or .bash_profile in the /etc/profile.d directory and remove the line that exports the variable.

.bashrc file:

nano ~/.bashrc

Then, search for the line export GNUPLOT_DRIVER_DIR and delete it. Then save the file.

In Windows

use the setx command to delete an environment variable.

setx GNUPLOT_DRIVER_DIR ""

You can find more information about environment variables and how to manage them in the following links:

krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
  • Is there a command that removes it 'permanently'? Kind of like unset, but works for more than just a single terminal instance? – default123 Jul 18 '23 at 19:45
  • default123 Short answer, NO. Read answers from e.g. @Trunk if your goal is to find and remove what ever is setting an environment variable. Though the first place to look is in your own shell initialization files (e.g. .bashrc, and .bash_profile for bash shell). – Steven the Easily Amused Jul 21 '23 at 00:40
1

LINUX

First find which script file defines and adds the variable to the environment.

Look in /etc files like profile, bash.bashrc, .bashrc, .bashrc_login, etc

And look in similarly named files in the user's home directory.

If it's not in any of those, it's likely that it's in some script file that was part of an installed package. For example, the package SDKMAN (for managing alternate SDK versions) creates a variable called DERBY_HOME. To find the script file creating it, apply the following search command to the /etc folder:

$ sudo egrep -lir THE_VAR_NAME /etc

This should produce some output like:

/etc/profile.d/jdk.sh
/etc/profile.d/jdk.csh

The separate file jdk.csh is needed for the C-shell environment if users have defaulted to it.

Once found it is simply a matter of navigating to the folder containing the script files, in this case here /etc/profile.d/ and then editing the files (with admin permission), removing the variable assignments and saving:

$ cd /etc/profile.d/

$ sudo gedit jdk.sh

$ sudo gedit jdk.sh

Of course, in this case the package setting the env variable is in use so I kept it.

But if the package were not in use and the env vars dead weight to the startup process, then it should be deleted.

WINDOWS

Use the process shown in this YouTube video.

Trunk
  • 742
  • 9
  • 24