How to set environment variables with a forward slash in the key

9.6k views Asked by At

Is there a way to export an environment variable with a slash in the name such as:

export /myapp/db/username=someval

This post indicates it should be possible but I cannot figure out valid syntax to do so.

For background:

I am using confd to produce config files from a template and key store. The typical stores (consul, etcd) use hierarchical keys such as /myapp/db/username. I would like to transparently allow for switching between using an environment variable based provider and a configuration store that leverages hierarchical keys.

2

There are 2 answers

2
chepner On BEST ANSWER

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

env "/myapp/db/username=someval" bash
0
John1024 On

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

While bash will refuse to create an environment variable named, for example, a/b, we can create it with python and subshells created by python will see it.

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 are put into a subshell. From that subshell, we can see that the creation of the environment variable was successful:

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

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