This site http://www.tldp.org/LDP/abs/html/gotchas.html#PARCHILDPROBREF indicates that a script cannot change its parent's environment variables.
So, (unless the script is sourced) a script cannot accidentally change a coincidentally named environment variable in its parent's environment.
However, namespace collisions can occur if a program relies on environment variables that it declares.
e.g. suppose I am unaware that vim uses the environment variable name "VIMRUNTIME". And, by coincidence, I happen to use that environment variable name for my own, unrelated, purposes. Then vim would get garbage from the VIMRUNTIME environment variable.
Are there any mechanisms or standard, well-known naming conventions to avoid this type of namespace collision? Of course, I can just do something like naming all my environment variables with some sort of prefix. But I would like to know if there are any well-known, formal standards for this. Or, is there some sort of namespace mechanism in Bash that I haven't found? Suggestions about other, related gotchas are encouraged in comments.
I don't think there's any standard mechanism, other than using a common prefix. For instance, ssh uses SSH_xxx for all its environment variables. Unfortunately, many legacy programs (e.g. shells) don't follow any kind of convention. And variables that are used across many different programs (e.g. TERM, PAGER) don't have a program name to use as a prefix.
If you follow the prefixing style, the chance of collision will be small. It's the best you can do.
Don't export it. There is a difference between shell variables and environment variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With