0

I have a file having a list of key-value pairs which I need to set as environment variables. I need to write a script which reads the key-value pairs from the file and set them as env variables. These env variables must be available for future application launches or new shell launch in the current login session. Is this possible?

One solution is writing the set of key-value pairs to /etc/environment file. But every time we run the script duplicate key-value pairs can be added to the /etc/environment file. How can this issue be solved?

codeforester
  • 39,467
  • 16
  • 112
  • 140
Stanly
  • 663
  • 1
  • 9
  • 26
  • 1
    Setting an environment variable more than a single time only overwrites the previous value, so that is not an issue. – arkascha Jan 08 '17 at 12:50
  • 1
    You can add the definitions to your personal `~/.bash_profile` or `.profile` script, whichever you use, this will set the variables once per session start. Certainly you can also keep them in a separate file and simply include that to one of the scripts mentioned before. – arkascha Jan 08 '17 at 12:51
  • It is _not_ possible to set environment variables in some shell and expect them to be available in _other_ shells (or processes) that were not spawned from the shell, regardless of whether they are in the same session or not. Environment variable inheritance is obviously only from parent to child processes. – arkascha Jan 08 '17 at 12:52
  • Another approach is calling `source ${mypath}/my_environ` where my_environ has only `key=value` lines. – Walter A Jan 08 '17 at 18:26
  • Possible duplicate of [Set environment variables from file](http://stackoverflow.com/questions/19331497/set-environment-variables-from-file) – jww Jan 08 '17 at 20:05
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. Also see [Where do I post questions about Dev Ops?](http://meta.stackexchange.com/q/134306) – jww Jan 08 '17 at 20:06

0 Answers0