问题描述
我有一个包含几个需要被设置为一个环境变量变量的脚本
I have a script that contains a couple of variables that need to be set as an environment variable
变量列表不断调整,修改它在我到底是不是一种选择。任何想法,我怎么会去这样做呢?
The list of variables change constantly and modifying it on my end is not an option. Any idea how I would go about doing it?
样本文件foo.sh
sample file foo.sh
FOO="FOOFOO"
BAR="BARBAR"
推荐答案
有一个变量和环境变量之间的差。如果执行。 foo.sh
和 foo.sh
包含行 FOO =值
,则变量 FOO
将当前进程分配。它不是一个环境变量。成为环境变量(因此提供给子壳),它必须被导出。然而,炮弹提供这使得所有的变量赋值推动变量的环境变量的选项,所以如果你简单地做:
There is a difference between a variable and an environment variable. If you execute . foo.sh
and foo.sh
contains the line FOO=value
, then the variable FOO
will be assigned in the current process. It is not an environment variable. To become an environment variable (and thus be available to sub-shells), it must be exported. However, shells provide an option which makes all variable assignments promote the variable to an environment variable, so if you simply do:
set -a
. foo.sh
set +a
然后foo.sh所有的变量赋值将在当前进程进行环境变量。请注意,这并不完全正确:在bash,导出一个变量使得它在当前shell的environement变量,但在其他shell(破折号,例如)导出变量不让它在当前shell的环境变量。 (它确实导致其在子shell环境然而,应当设置它。)
then all variable assignments in foo.sh will be made environment variables in the current process. Note that this is not strictly true: in bash, exporting a variable makes it an environement variable in the current shell, but in other shells (dash, for example) exporting the variable does not make it an environment variable in the current shell. (It does cause it to be set it in the environment of subshells, however.)
这篇关于出口变量在另一个文件中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!