bash shell 编程中的“导出”有什么作用?

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

What does "export" do in shell programming?

bashshellzsh

提问by John Bachir

As far as I can tell, variable assignment is the same whether it is or is not preceded by "export". What's it for?

据我所知,变量赋值是相同的,无论它前面是否有“export”。这是为了什么?

回答by Jonathan Leffler

Exported variables such as $HOMEand $PATHare available to (inherited by) other programs run by the shell that exports them (and the programs run by those other programs, and so on) as environment variables. Regular (non-exported) variables are not available to other programs.

导出的变量,例如$HOME$PATH可用于(由其继承)由外壳程序运行的其他程序,这些程序将它们(以及由这些其他程序运行的程序等)作为环境变量导出。其他程序无法使用常规(非导出)变量。

$ env | grep '^variable='
$                                 # No environment variable called variable
$ variable=Hello                  # Create local (non-exported) variable with value
$ env | grep '^variable='
$                                 # Still no environment variable called variable
$ export variable                 # Mark variable for export to child processes
$ env | grep '^variable='
variable=Hello
$
$ export other_variable=Goodbye   # create and initialize exported variable
$ env | grep '^other_variable='
other_variable=Goodbye
$

For more information, see the entry for the exportbuiltinin the GNU Bash manual, and also the sections on command execution environmentand environment.

有关更多信息,请参阅GNU Bash 手册中的export内建条目,以及有关命令执行环境环境的部分

Note that non-exported variables will be available to subshells run via ( ... )and similar notations because those subshells are direct clones of the main shell:

请注意,非导出变量将可用于通过( ... )和类似符号运行的子外壳,因为这些子外壳是主外壳的直接克隆:

$ othervar=present
$ (echo $othervar; echo $variable; variable=elephant; echo $variable)
present
Hello
elephant
$ echo $variable
Hello
$

The subshell can change its own copy of any variable, exported or not, and may affect the values seen by the processes it runs, but the subshell's changes cannot affect the variable in the parent shell, of course.

子shell 可以更改它自己的任何变量的副本,无论是否导出,并且可能会影响它运行的进程看到的值,但是子shell 的更改当然不会影响父shell 中的变量。

Some information about subshells can be found under command groupingand command execution environmentin the Bash manual.

关于子shell的一些信息可以在Bash手册的命令分组命令执行环境下找到。

回答by jcomeau_ictx

it makes the assignment visible to subprocesses.

它使分配对子进程可见。

jcomeau@intrepid:~/rentacoder/bin2txt$ foo=bar
jcomeau@intrepid:~/rentacoder/bin2txt$ bash -c 'echo $foo'

jcomeau@intrepid:~/rentacoder/bin2txt$ export foo
jcomeau@intrepid:~/rentacoder/bin2txt$ bash -c 'echo $foo'
bar

回答by paxdiablo

Well, it generally depends on the shell. For bash, it marks the variable as "exportable" meaning that it will show up in the environment for any child processes you run.

嗯,这通常取决于外壳。对于bash,它将变量标记为“可导出”,这意味着它将显示在您运行的任何子进程的环境中。

Non-exported variables are only visible from the currentprocess (the shell).

非导出变量仅在当前进程(shell)中可见。

From the bashman page:

bash手册页:

export [-fn] [name[=word]] ...
export -p

The supplied names are marked for automatic export to the environment of subsequently executed commands.

If the -foption is given, the names refer to functions. If no names are given, or if the -poption is supplied, a list of all names that are exported in this shell is printed.

The -noption causes the export property to be removed from each name.

If a variable name is followed by =word, the value of the variable is set to word.

exportreturns an exit status of 0 unless an invalid option is encountered, one of the names is not a valid shell variable name, or -fis supplied with a name that is not a function.

export [-fn] [name[=word]] ...
export -p

提供的名称被标记为自动导出到随后执行的命令的环境中。

如果-f给出了选项,则名称指的是函数。如果没有给出名称,或者-p提供了该选项,则会打印在此 shell 中导出的所有名称的列表。

-n选项会导致从每个名称中删除导出属性。

如果变量名后跟=word,则变量的值设置为word

export除非遇到无效选项、名称之一不是有效的 shell 变量名称或-f提供的名称不是函数,否则返回退出状态 0 。

You can also set variables as exportable with the typesetcommand and automatically mark all future variable creations or modifications as such, with set -a.

您还可以使用该typeset命令将变量设置为可导出,并使用set -a.