How to export dot separated environment variables

13,288

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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

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

Share:
13,288
Petro Semeniuk
Author by

Petro Semeniuk

Updated on July 23, 2022

Comments

  • Petro Semeniuk
    Petro Semeniuk almost 2 years

    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?

  • Adrian Pronk
    Adrian Pronk about 11 years
    @MarkEdgar: Yes, env does work. I just didn't know it could be used to set environment variables, I've only ever used it to display environment variables.
  • divideByZero
    divideByZero almost 9 years
    I had to set env variable for Java server, so for me worked export JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=somevalue"