Linux 如何删除导出的环境变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6877727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:21:36  来源:igfitidea点击:

How do I delete an exported environment variable?

linuxenvironment-variablesunset

提问by A. K.

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

在安装 gnuplot 之前,我设置了环境变量GNUPLOT_DRIVER_DIR = /home/gnuplot/build/src. 在安装过程中,出了点问题。

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

我想删除GNUPLOT_DRIVER_DIR环境变量。我怎样才能实现它?

采纳答案by Peder Klingenberg

unsetis the command you're looking for.

unset是您要查找的命令。

unset GNUPLOT_DRIVER_DIR

回答by Eric Leschinski

Walkthrough of creating and deleting an environment variable in bash:

在 bash 中创建和删除环境变量的演练:

Test if the DUALCASE variable exists:

测试 DUALCASE 变量是否存在:

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.

DUALCASE 导出的环境变量被删除。

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 bashcommand cleared all the local variables but not environment variables.

exec bash命令清除所有局部变量,但不清除环境变量。

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 bashcommand cleared all the environment variables to default on login.

env -i bash命令在登录时将所有环境变量清除为默认值。

回答by Nilesh K.

this may also work.

这也可能有效。

export GNUPLOT_DRIVER_DIR=

回答by G Eitan

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:

在 C shell (csh/tcsh) 中有两种设置环境变量的方法:

  1. set x = "something"
  2. setenv x "something"
  1. set x = "something"
  2. setenv x "something"

The difference in the behaviour is that variables set with setenvcommand are automatically exported to subshell while variable set with setaren't.

行为的不同之处在于,使用setenv命令设置的变量会自动导出到子 shell,而使用set 设置的变量则不会。

To unset a variable set with set, use

要使用set取消设置变量,请使用

unset x

To unset a variable set with setenv, use

要使用setenv取消设置变量,请使用

unsetenv x

Note:in all the above, I assume that the variable name is 'x'.

注意:在以上所有内容中,我假设变量名称是“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

https://www.cyberciti.biz/faq/unix-linux-difference-between-set-and-setenv-c-shell-variable/ https://www.oreilly.com/library/view/solarism-7-参考/0130200484/0130200484_ch18lev1sec24.html

回答by Rishabh Bohra

As mentioned in the above answers, unset GNUPLOT_DRIVER_DIRshould work if you have used exportto set the variable. If you have set it permanently in ~/.bashrcor ~/.zshrcthen simply removing it from there will work.

正如上面的答案中提到的,unset GNUPLOT_DRIVER_DIR如果您曾经export设置过变量,应该可以使用。如果您已将其永久设置,~/.bashrc或者~/.zshrc只需将其从那里移除即可。