How do I delete an exported environment variable?

LinuxEnvironment VariablesUnset

Linux Problem Overview


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?

Linux Solutions


Solution 1 - Linux

unset is the command you're looking for.

unset GNUPLOT_DRIVER_DIR

Solution 2 - Linux

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.

Solution 3 - Linux

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:

Solution 4 - Linux

This may also work.

export GNUPLOT_DRIVER_DIR=

Solution 5 - Linux

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionA. K.View Question on Stackoverflow
Solution 1 - LinuxPeder KlingenbergView Answer on Stackoverflow
Solution 2 - LinuxEric LeschinskiView Answer on Stackoverflow
Solution 3 - LinuxG EitanView Answer on Stackoverflow
Solution 4 - LinuxNilesh K.View Answer on Stackoverflow
Solution 5 - LinuxRishabh BohraView Answer on Stackoverflow