Possible Duplicate:
Exporting an Environment Variable in Ruby
I need to set several environment properties from inside of ruby script.
Normally, in bash, I do the following:
$ export SOME_VAR=some_value
But in ruby, following (obviously) doesn't work:
irb(main):002:0> `export SOME_VAR=some_value`
(irb):2: command not found: export ASDF=1
=> ""
Is there a way to do it?
According to http://ruby.about.com/od/rubyfeatures/a/envvar.htm, you can just write:
ENV['SOME_VAR'] = 'some_value'
If you don't want this value to persist after script is finished, you can alter ENV
directly.
ENV['SOME_VAR'] = 'some_value'
puts ENV['SOME_VAR']
# => some_value
If you do want persistence, then you probably (in addition to this) have to write this var to a ~/.bashrc
or similar file on your system.
Try `ENV['SOME_VAR'] = 'some_value'.
You cannot make the effects of this persist in the environment executing the script, after the script is finished.
A trick that is being discussed in the comments to my answer, is to print valid shell code to the console, from your ruby script — this is not what you need, but it may be useful to know it could work that way too.
$ echo "puts 'export foo=bar'" > test.rb
$ echo $foo
$ source <(ruby test.rb)
$ echo $foo
bar
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