csh 中“setenv”的范围与 bash 中的“export”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17351010/
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-09-09 23:53:41  来源:igfitidea点击:

Scope of "setenv" in csh Versus "export" in bash

bashshellexportcshsetenv

提问by Dao Lam

From what I've read, setenvin csh and exportin bash are equivalent. However, I found some strange issues while working with the two.

从我读过的内容来看,setenv在 csh 和exportbash 中是等效的。但是,在与两者合作时,我发现了一些奇怪的问题。

When I open putty and start typing:

当我打开腻子并开始输入时:

setenv TEMP yes
echo $TEMP  //this give me "yes"

then I go to bash and type

然后我去 bash 并输入

echo $TEMP //this give me "yes" as well

However, if I do it in the opposite order, it wouldn't give the same results. Specifically, when I go to bash first and type

但是,如果我以相反的顺序执行此操作,则不会给出相同的结果。具体来说,当我先去 bash 并输入

export TEMP=no
echo $TEMP //this give me "no"

then I go back to csh and type

然后我回到 csh 并输入

echo $TEMP // this give me "Undefined Variable"

Shouldn't it give me "no" as well? Am I missing something?

它不应该也给我“不”吗?我错过了什么吗?

Thank you!

谢谢!

回答by rici

Exporting a variable means that a copy of that variable is placed into the environment of any newly created child processes. It is a copy of the variable; if the child process modifies the variable, the parent does not see the modification. Moreover, if a child exports a variable, it does not become visible in the parent.

导出变量意味着将该变量的副本放入任何新创建的子进程的环境中。它是变量的副本;如果子进程修改了变量,则父进程看不到修改。此外,如果子项导出变量,它在父项中是不可见的。

Hence, your two cases are asymmetrical. When you start in csh, export a variable, and then start bash, bash sees the exported variable. When you then export a new variable in bash and exit from bash to go back to csh, all of the variables created in the bash session disappear.

因此,您的两个案例是不对称的。当你在csh中启动,导出一个变量,然后启动bash,bash看到的是导出的变量。当您随后在 bash 中导出一个新变量并从 bash 退出以返回到 csh 时,在 bash 会话中创建的所有变量都将消失。

If you were to export a variable in bash and then start up a child csh (by typing csh), you would almost certainly see the exported variable.

如果您要在 bash 中导出一个变量,然后启动一个子 csh(通过键入csh),您几乎肯定会看到导出的变量。