In git bash (on windows) how can I set an environment variable with special characters in the key? Like some.var:.
The following does not work: export "some.var:"=123
-
You really should have included why you need to do this since it smells like an XY Problem.Kurtis Rader– Kurtis Rader2021-07-15 01:54:36 +00:00Commented Jul 15, 2021 at 1:54
-
well TBH it's something we've been using for years in production apps, which suddenly broke and I needed to test it - so it seems like discussing the reasoning for it would be a waste at this point.joniba– joniba2021-07-15 15:40:31 +00:00Commented Jul 15, 2021 at 15:40
1 Answer 1
export requires a valid bash identifier, so it can only define a subset of legal environment strings.
If you have a program that requires a name that is not a valid identifier, you'll need to use an external program to create the necessary environment. For example:
$ env -i "some.var:=123" env
some.var:=123
env by itself reports its inherited environment. With a command, it runs that command in a possibly modified environment. -i clears the initial environment (so that we aren't flooded with irrelevant environment string; in practice, you can omit it if you want to inherit the current environment as well), and the string defines a name some.var: to have the value 123).
my_cmd () { env "some.var:=123" my_cmd; }. (No recursion here, as env will be looking for a command in your path, not a shell function of the same name.)