How to set environment variables using a slash in a key

Is there a way to export an environment variable with a slash in the name, for example:

export /myapp/db/username=someval

This post indicates that this should be possible, but I cannot figure out what syntax is for this.

For background:

I use confd to create configuration files from a template and keystore. Typical stores (consul, etcd) use hierarchical keys, such as /myapp/db/username . I would like to transparently allow the transition between using provider based on environment variables and a configuration repository that uses hierarchical keys.

+5
source share
2 answers

export only marks valid shell identifiers for export to the environment, not any string that could create a valid name / value pair in the environment. You can use env to create a new shell instance with such an environment.

 env "/myapp/db/username=someval" bash 
+1
source

Yes, you can export such an environment variable, but not from the bash export statement.

While bash refuses to create an environment variable called, for example, a/b , we can create it using python and subshells created by python, and see this.

As an example, consider the following python command:

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

If we run this command, we put it in a subshell. From this subshell, you can see that creating the environment variable was successful:

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

(At this point, you can exit the subshell (type exit or ctrl-D) to return to the python program, which will exit and return us to the main shell.)

+4
source

All Articles