bash 导出在另一个文件中定义的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14904142/
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
Export variables defined in another file
提问by philipdotdev
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?
变量列表不断变化,我无法修改它。知道我将如何去做吗?
sample file foo.sh
示例文件 foo.sh
FOO="FOOFOO"
BAR="BARBAR"
回答by William Pursell
There is a difference between a variable and an environment variable. If you execute . foo.shand foo.shcontains the line FOO=value, then the variable FOOwill 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:
变量和环境变量是有区别的。如果您执行. foo.sh并foo.sh包含 line FOO=value,则该变量FOO将在当前进程中分配。它不是环境变量。要成为环境变量(从而可用于子 shell),它必须被导出。但是,shell 提供了一个选项,可以让所有变量赋值都将变量提升为环境变量,因此如果您只需执行以下操作:
set -a
. foo.sh
set +a
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.) However, in the context of the shell, it does not really matter if a variable is an environment variable in the current process. If it is exported (and therefore set in the environment of any sub-processes), a variable that is not in the environment is functionally equivalent to an environment variable.
那么 foo.sh 中的所有变量赋值都将成为当前进程中的环境变量。请注意,这并非严格正确:在 bash 中,导出变量使其成为当前 shell 中的环境变量,但在其他 shell(例如,dash)中,导出变量不会使其成为当前 shell 中的环境变量。(然而,它确实会导致在子外壳的环境中设置它。)但是,在外壳的上下文中,变量是否是当前进程中的环境变量并不重要。如果导出(并因此在任何子流程的环境中设置),则不在环境中的变量在功能上等同于环境变量。
回答by chepner
Are you looking for the '.' command (also spelled 'source' in bash):
您在寻找“.”吗?命令(在 中也拼写为“源” bash):
source foo.sh
Since you need to export them to the environment as well, you may need to do a little extra work:
由于您还需要将它们导出到环境中,因此您可能需要做一些额外的工作:
while read -r line; do
export "$line"
done < foo.sh
回答by martinec
If you are looking to export only variables with a common prefix, e.g.:
如果您只想导出具有公共前缀的变量,例如:
BAZ_FOO="FOOFOO"
BAZ_BAR="BARBAR"
You could simply do this:
你可以简单地这样做:
. foo.sh
variable_list=()
variable_list=( $(set -o posix ; set |\
grep "BAZ_" |\
cut -d= -f1) )
for variable in "${variable_list[@]}"
do
export "$variable"
done

