I am newbie in Ruby and want to export environment variable permanently to host system.
Right now I have following ruby script on which I am working. In this script there is ENV command that adds URL env var to current execution env and it works fine. But I want to make this change persistent to hostmachine, in my case its linux. how can I do that?
require 'singleton'
java_import com.test.TestClass;
class RubyClient
include Singleton
def initialize
ENV['URL'] = 'http://localhost'
end
end
def test_client
puts TestClass.getEnv
end
class TestClass{
public String getEnv({
return StringUtils.trimToNull(System.getenv("URL"));
}
}
CodePudding user response:
Use Direnv or Dotenv
This is not how environment variables work. You export them in the current shell, and they are inherited by subshells. You can't really export variables from subshells back to a parent shell.
There are tools that make it seem like you can do this (e.g. direnv) but if you want a value to be read into a parent shell you need to ensure that the shell is reading in a stored value from disk before each command. Direnv or dotenv hooked into your shell will accomplish this from a pragmatic perspective, but the parent shell itself never truly inherits from a subshell.
CodePudding user response:
This is impossible. Environment variables apply to the currently running process, and only to the currently process. Child processes started by this process will initially inherit their parent's environment, but can (and often do) override it with its own.
One process cannot change another process's environment. This has nothing to do with Ruby or Java, this is a fundamental security property of all Unices as well as many other OSs that have a comparable concept of "environment".