How to export dot separated environment variables

前端 未结 4 2075
粉色の甜心
粉色の甜心 2021-02-14 17:56

Execution of

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

Gives me

-bash: export: `property.name=property.value\         


        
相关标签:
4条回答
  • 2021-02-14 18:17

    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 -D option, e.g. you can set system property with dot like this -Dproperty.name=property.value.

    0 讨论(0)
  • 2021-02-14 18:21

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

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

    0 讨论(0)
  • 2021-02-14 18:37

    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:

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

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

    Edit: @MarkEdgar commented that the env command will work too:

     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.

    0 讨论(0)
  • 2021-02-14 18:43

    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:

    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.

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

    0 讨论(0)
提交回复
热议问题