bash *导出*所有变量从键=值文件到shell
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32761478/
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* all variables from key=value file to shell
提问by uml?ute
If I want to inherit environment variables to child processes, i do something like:
如果我想将环境变量继承给子进程,我会执行以下操作:
export MYVAR=tork
Assume I have a a file site.conf
containing assignments of values (that can contain spaces) to variables:
假设我有一个文件,site.conf
其中包含对变量的值(可以包含空格)的赋值:
EMAIL="[email protected]"
FULLNAME="Master Yedi"
FOO=bar
Now I would like to process this file whenever I open a new shell (e.g. with some code in ~/.bashrc
or ~/.profile
), so that any processes started from within that newly opened shell will inherit the assignments via environmental variables.
现在,每当我打开一个新的 shell(例如在~/.bashrc
或 中使用一些代码~/.profile
)时,我都想处理这个文件,以便从新打开的 shell 中启动的任何进程都将通过环境变量继承分配。
The obvious solution would be to prefix each line in site.conf
with an export
and just source the file. However I cannot do this since the file is also read (directly) by some other applications, so the format is fixed.
显而易见的解决方案是在每一行前面site.conf
加上一个前缀,export
并且只提供文件的来源。但是我不能这样做,因为文件也被其他一些应用程序(直接)读取,所以格式是固定的。
I tried something like
我试过类似的东西
cat site.conf | while read assignment
do
export "${assignment}"
done
But this doesn't work, for various reasons (the most important being that export
is executed in a subshell, so the variable will never be exported to the children of the calling shell).
但这不起作用,出于各种原因(最重要的export
是在子 shell 中执行,因此变量永远不会导出到调用 shell 的子级)。
Is there a way to programmatically export
unknown variables in bash?
有没有办法export
在bash中以编程方式未知变量?
回答by chepner
Run set -a
before sourcing the file. This marks all new and modified variables that follow for export automatically.
set -a
在获取文件之前运行。这会标记所有新的和修改后的变量,以便自动导出。
set -a
source site.conf
set +a # Require export again, if desired.
The problem you observed is that the pipe executes the export
in a subshell. You can avoid that simply by using input redirection instead of a pipe.
您观察到的问题是管道export
在子外壳中执行。您可以简单地通过使用输入重定向而不是管道来避免这种情况。
while read assignment; do
export "$assignment"
done < site.conf
This won't work, however, if (unlikely though it is) you have multiple assignments on one line, such as
但是,如果(虽然不太可能)您在一行上有多个分配,则这将不起作用,例如
EMAIL="[email protected]" FULLNAME="Master Yedi"
回答by anubhava
Problem is cat site.conf | while read assignment
using pipes.
问题是cat site.conf | while read assignment
使用管道。
Pipes create a sub-shell, hence the variable created using export
get created in a sub-shell and are not available in your current shell.
管道创建一个子外壳,因此使用export
get 创建的变量在子外壳中创建,并且在您当前的外壳中不可用。
You can just do:
你可以这样做:
source $HOME/site.conf
from your ~/.bashrc
to export all the variables and make them available in shell.
从您~/.bashrc
导出所有变量并使它们在 shell 中可用。