Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any standard mechanisms or conventions to prevent Bash environment variable name collisions?

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.

like image 728
user2141130 Avatar asked Jun 26 '13 20:06

user2141130


2 Answers

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.

like image 142
Barmar Avatar answered Oct 13 '22 15:10

Barmar


Don't export it. There is a difference between shell variables and environment variables.

like image 31
Mark Edgar Avatar answered Oct 13 '22 15:10

Mark Edgar