3

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

2
  • You really should have included why you need to do this since it smells like an XY Problem. Commented 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. Commented Jul 15, 2021 at 15:40

1 Answer 1

5

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).

Sign up to request clarification or add additional context in comments.

thanks, this works, but is there a way to persist this variable so I don't need to redefine it before every command?
Unfortunately, no; the shell itself has no way of creating environment variables other than exporting ordinary shell variables. You would need to start your shell using this technique, inheriting the value from the parent rather then setting them via your shell configuration files.
In practice, you could define a wrapper around the command you want to run, something like 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.)

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.