6

Is there a way to export an environment variable with a slash in the name such as:

export /myapp/db/username=someval

This post indicates it should be possible but I cannot figure out valid syntax to do so.

For background:

I am using confd to produce config files from a template and key store. The typical stores (consul, etcd) use hierarchical keys such as /myapp/db/username. I would like to transparently allow for switching between using an environment variable based provider and a configuration store that leverages hierarchical keys.

4

2 回答 2

4

是的,您可以导出这样的环境变量,但不能从 bashexport语句中导出。

虽然bash会拒绝创建一个名为的环境变量,例如,a/b我们可以用python创建它,python创建的子shell会看到它。

例如,考虑以下 python 命令:

$ python -c 'import os; os.environ["a/b"]="2"; os.system("/bin/bash");'

如果我们运行这个命令,我们会被放入一个子shell。从那个子shell中,我们可以看到环境变量的创建是成功的:

$ printenv | grep a/b
a/b=2

(此时,可能想要退出子shell(键入exit或ctrl-D)以返回python程序,该程序将退出并将我们返回到主shell。)

于 2015-06-09T03:15:58.580 回答
3

export仅标记要导出到环境中的有效 shell 标识符,而不是任何可以在环境中形成有效名称/值对的字符串。不过,您可以使用这样的环境env创建一个的shell 实例。

env "/myapp/db/username=someval" bash
于 2015-06-09T12:32:56.547 回答