BASH:“导出 k=1”与“k=1”之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12561654/
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: difference between "export k=1" vs. "k=1"
提问by lashgar
I am going to write an script and It looks there is no difference between:
我要写一个脚本,看起来没有区别:
export k=1
and
和
k=1
Am I right?
我对吗?
采纳答案by cdarke
Every process, even on Windows, has a block of memory known as the environment block, this contains environment variables. When a new process is created, by default, the environment block of the parent process is copied to the child, so environment variables are a simple way of passing text data to a child process.
每个进程,甚至在 Windows 上,都有一个称为环境块的内存块,它包含环境变量。创建新进程时,默认情况下会将父进程的环境块复制到子进程,因此环境变量是将文本数据传递给子进程的一种简单方式。
The exportcommand creates an environment variable, or converts an ordinary local variable into an environment variable. In the C-shell, one of its few redeeming features is that it uses a different syntax for environment variables (setenv) to local variables (set). Bourne shell derivatives, like Bash and Korn shell, hide all that.
该export命令创建环境变量,或将普通局部变量转换为环境变量。在 C-shell 中,它为数不多的补救功能之一是它对环境变量 ( setenv) 和局部变量 ( set)使用不同的语法。Bourne shell 衍生物,如 Bash 和 Korn shell,隐藏了所有这些。
Currently, only simple values can be passed, so items like arrays are not supported (it just exports the first element). Variable attributes, set using define, are also not exported unless the child process is a shell of the same type, i.e. another instance of bash. This also applies to exported functions, although it is possible to sometimes hack this between shells of different types (using eval).
目前,只能传递简单的值,因此不支持数组之类的项目(它只导出第一个元素)。define除非子进程是相同类型的 shell,即 bash 的另一个实例,否则也不会导出使用设置的变量属性。这也适用于导出的函数,尽管有时可以在不同类型的 shell 之间破解它(使用eval)。
In Bash (and others) there is a shell setting called allexportwhich means all variables are environment variables - probably a bad idea to se generally. You can supply a different environemnt block from languages like C using execve, but from the shell you need a program like env, see man envfor details.
在 Bash(和其他)中,有一个 shell 设置被调用allexport,这意味着所有变量都是环境变量——通常来说这可能是一个坏主意。您可以使用 为 C 等语言提供不同的环境块execve,但在 shell 中您需要一个类似 的程序env,请参阅man env有关详细信息。
回答by Brian Agnew
exportmakes the variable available to subprocesses.
export使变量可用于子进程。
That is, if you spawn a new process from your script, the variable kwon't be available to that subprocess unless you exportit. Note that if you change this variable in the subprocess that change won'tbe visible in the parent process.
也就是说,如果您从脚本中生成一个新进程,则该变量k将无法用于该子进程,除非您使用export它。请注意,如果您在子流程中更改此变量,则该更改在父流程中将不可见。
See section 3.2.3 of this docfor more detail.
有关更多详细信息,请参阅本文档的第 3.2.3 节。
回答by Mihai Maruseac
I've created a simple script to show the difference:
我创建了一个简单的脚本来显示差异:
$ cat script.sh
echo $answer
Let's test without export
让我们测试一下 export
$ answer=42
$ ./script.sh
$ . script.sh
42
The value is known only if using the sameprocess to execute the script (that is, the same bashinstance, using source/ .)
仅当使用相同的进程执行脚本(即相同的bash实例,使用source/ .)时才知道该值
Now, using export:
现在,使用export:
$ export answer=42
$ ./script.sh
42
$ . script.sh
42
The value is known to the subprocess.
该值是子进程已知的。
Thus, if you want the value of a variable to be known by subprocesses then you should use export.
因此,如果您希望子进程知道变量的值,那么您应该使用export.

