bash 在当前环境中创建带点的环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29753126/
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
Create environment variable with dot in the current environment
提问by razpeitia
I know that you can create environment variables with the command env
.
我知道您可以使用命令创建环境变量env
。
For example:
例如:
env A.B=D bash
The problem is for env a command is required thus creating a new subprocess.
问题是 env 需要一个命令,从而创建一个新的子进程。
回答by rici
Bash does not allow environment variables with non-alphanumeric characters in their names (aside from _). While the environment may contain a line such as A.B=D
, there is no requirement that a shell be able to make use of it, and bash will not. Other shells may be more flexible.
Bash 不允许名称中包含非字母数字字符的环境变量(除了_)。虽然环境可能包含诸如 之类的行A.B=D
,但并不要求 shell 能够使用它,而 bash 则不会。其他壳可能更灵活。
Utilities which make use of oddly-named environment variables are discouraged, but some may exist. You will need to use env
to create such an environment variable. You could avoid the subprocess with exec env bash
but it won't save you much in the way of time or resources.
不鼓励使用名称奇怪的环境变量的实用程序,但可能存在一些实用程序。您将需要使用env
来创建这样的环境变量。您可以避免使用子流程,exec env bash
但它不会为您节省太多时间或资源。
回答by glenn Hymanman
rici has the correct answer. To demonstrate the difference required to access such environment entries, bash requires you to parse the environment as text:
rici 有正确的答案。为了演示访问此类环境条目所需的差异,bash 要求您将环境解析为文本:
$ env A.B=C perl -E 'say $ENV{"A.B"}'
C
$ env A.B=C bash -c 'val=$(env | grep -oP "^A\.B=\K.*"); echo "$val"'
C