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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:37:38  来源:igfitidea点击:

*export* all variables from key=value file to shell

bashshellenvironment-variablesexport

提问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.confcontaining 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 ~/.bashrcor ~/.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.confwith an exportand 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 exportis 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 exportunknown variables in bash?

有没有办法export在bash中以编程方式未知变量?

回答by chepner

Run set -abefore 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 exportin 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 assignmentusing pipes.

问题是cat site.conf | while read assignment使用管道。

Pipes create a sub-shell, hence the variable created using exportget created in a sub-shell and are not available in your current shell.

管道创建一个子外壳,因此使用exportget 创建的变量在子外壳中创建,并且在您当前的外壳中不可用。

You can just do:

你可以这样做:

source $HOME/site.conf

from your ~/.bashrcto export all the variables and make them available in shell.

从您~/.bashrc导出所有变量并使它们在 shell 中可用。