0

I want to create env parameter that the key is a:b or a@b I need to do it from bash script or from terminal , it should work from linux or windows

when I tried it export a:b=c I got an error

not a valid identifier

When I tried export tempKey = a:b then It worked but then I didn't know how to use the value a:b to create it as key

Could you please advise ?

13
  • 1
    In standard shells, variable names consist of alphanumerics plus underscore, not starting with a digit. The names cannot contain a colon or at-sign. Some shells allow some extensions to this default character set. If yours doesn't, there's nothing for it but to change shell, either by adapting the one you're using (get the source code, change it, recompile it) or by getting a shell that already supports the extended notation. If you need portable shell scripts, don't use the extended notation. Commented Oct 20, 2018 at 7:08
  • Is it possible to write in shell script ? Commented Oct 20, 2018 at 7:09
  • 1
    Is it possible to write what in a shell script? You got 'not a valid identifier' as an error, so your shell won't allow you to use a:b as a variable name. Why do you think you need this facility? The default solution is to use underscore in place of punctuation: export a_b=c is perfectly kosher in most Bourne-shell deriviatives (though not in the original Bourne shell). Commented Oct 20, 2018 at 7:10
  • to write not from git bash but from shell script export a:b=c Commented Oct 20, 2018 at 7:11
  • I need it for npm env to change the environment value so the key should be npm_config_key=value the probelm that my key is with annotation scope meaning @myScope:key=value so how I can do it and add environment variable in this case? Commented Oct 20, 2018 at 7:20

1 Answer 1

1

None of the commonly used unix shells will let you create a var whose name includes characters not legal in an identifier (typically letters, digits and underscore). The simplest workaround is to use the env command since it doesn't impose any restrictions on the strings it puts in the environment. For example, env a:b=c a_cmd where a_cmd is whatever command needs that environment string. If you want it to be part of the shell's environment do exec env a:b=c $SHELL. Obviously the new shell won't be able to use that var since $a:b is not a valid var reference even if you enclose the var name in braces.

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

Comments

Your Answer

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.