bash 如何导出点分隔的环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15016403/
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 04:38:01  来源:igfitidea点击:

How to export dot separated environment variables

macosbashunix

提问by Petro Semeniuk

Execution of

执行

user@EWD-MacBook-Pro:~$ export property.name=property.value

Gives me

给我

-bash: export: `property.name=property.value': not a valid identifier

Is it possible to have system properties with dot inside? If so how do that?

是否可以在系统属性中使用点?如果是这样怎么办?

回答by Adrian Pronk

As others have said, bash doesn't allow it so you'll have to use your favourite scripting language to do it. For example, in Perl:

正如其他人所说,bash 不允许这样做,因此您必须使用自己喜欢的脚本语言来执行此操作。例如,在 Perl 中:

perl -e '$ENV{"property.name"} = "property.value"; system "bash"'

This will fire up a subshell bash with the property.nameenvironment variable set, but you still can't access that environment variable from bash (although your program will be able to see it).

这将启动一个property.name设置了环境变量的子 shell bash ,但您仍然无法从 bash 访问该环境变量(尽管您的程序将能够看到它)。

Edit: @MarkEdgar commented that the envcommand will work too:

编辑:@MarkEdgar 评论说env命令也可以工作:

 env 'property.name=property.value' bash # start a subshell, or
 env 'property.name=property.value' command arg1 arg2 ...   # Run your command

As usual, you only require quotes if you need to protect special characters from the shell or want to include spaces in the property name or value.

像往常一样,如果您需要保护 shell 中的特殊字符或希望在属性名称或值中包含空格,则只需要引号。

回答by umghhh

I spent better part of this afternoon trying to figure out how to access some property set by Jenkins (to pass a job parameters jenkins uses property format with a dot) - this was a good hint from Adrian and yes it works for reading properties in the script too. I was at a loss as to what to do but then I tried:

我今天下午的大部分时间都在试图弄清楚如何访问 Jenkins 设置的一些属性(传递作业参数 jenkins 使用带点的属性格式)——这是 Adrian 的一个很好的提示,是的,它适用于读取 Jenkins 中的属性脚本也是。我不知所措,但后来我尝试了:

var=`perl -e 'print $ENV{"property.name"};print "\n";'`

This worked quite well actually. But of course that works in a shell that was started with the property set in the environment already i.e. in Adrian's example this could work in a script started from bash instance invoked in perl example he provided. It would not if this perl sniplet was put in the same shell only directly after his perl example.

这实际上工作得很好。但是,当然,这可以在以环境中设置的属性启动的 shell 中工作,即在 Adrian 的示例中,这可以在从他提供的 perl 示例中调用的 bash 实例启动的脚本中工作。如果这个 perl sniplet 只是在他的 perl 示例之后直接放在同一个 shell 中,则不会。

At least I learnt something this afternoon so not all this time is a waste.

至少我今天下午学到了一些东西,所以不是所有的时间都是浪费。

回答by Wenbing Li

If you export those properties to run an application, some programs can support setting system property as an option, and allow .in the property name.

如果导出这些属性来运行应用程序,某些程序可以支持将系统属性设置为选项,并允许 .在属性名称中。

In Java world, most of tools support setting system property by -Doption, e.g. you can set system property with dot like this -Dproperty.name=property.value.

在 Java 世界中,大多数工具都支持通过-D选项设置系统属性,例如您可以像这样用点设置系统属性-Dproperty.name=property.value

回答by sashang

Bash only permits '_' and alpha numeric characters in variable names. The '.' isn't permitted.

Bash 只允许在变量名称中使用 '_' 和字母数字字符。这 '。' 不允许。

http://tldp.org/LDP/abs/html/gotchas.html

http://tldp.org/LDP/abs/html/gotchas.html