BASH:在 .bashrc 中设置变量时是否需要“导出”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6297271/
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
BASH: Is "export" needed, when setting a variable in .bashrc?
提问by casper
I wonder if there is a need to use "export" when setting a variable in .bashrc.
我想知道在.bashrc中设置变量时是否需要使用“导出”。
In my tests editing .bashrc there was no difference between
在我的测试中编辑 .bashrc 之间没有区别
foo=bar
and
和
export foo=bar
In both cases, after login "echo $foo" outputs "bar".
在这两种情况下,登录后“echo $foo”输出“bar”。
I am using Debian Squeeze, if that matters.
如果这很重要,我正在使用 Debian Squeeze。
Thank you guys in advance.
提前谢谢你们。
采纳答案by Blagovest Buyukliev
Try creating a shell script that accesses the foovariable.
尝试创建一个访问foo变量的 shell 脚本。
If foowas export'ed, it will be visible in the shell script, otherwise it won't.
如果foo被export'ed,它将在 shell 脚本中可见,否则它不会。
回答by Hank Gay
SuperUser has this covered.
超级用户已经涵盖了这一点。
Short answer: exportmakes sure the environment variable is set in child processes. If you don't export, it's only available in the same process/interactive session.
简短回答:export确保在子进程中设置了环境变量。如果您不导出,则它仅在同一进程/交互式会话中可用。
回答by DrPppr242
It's preferable because exported variables get passed to child processes (programs launched from that shell). Without the export command those variables only apply to the shell itself and not processes launched from the shell
这是可取的,因为导出的变量会传递给子进程(从该 shell 启动的程序)。如果没有 export 命令,这些变量只适用于 shell 本身,而不适用于从 shell 启动的进程

