Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to export environment property from ruby script? [duplicate]

Tags:

bash

ruby

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?

like image 741
Rogach Avatar asked Feb 19 '12 17:02

Rogach


3 Answers

According to http://ruby.about.com/od/rubyfeatures/a/envvar.htm, you can just write:

ENV['SOME_VAR'] = 'some_value'
like image 113
ruakh Avatar answered Oct 01 '22 20:10

ruakh


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.

like image 22
Sergio Tulentsev Avatar answered Oct 01 '22 20:10

Sergio Tulentsev


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
like image 23
Irfy Avatar answered Oct 01 '22 19:10

Irfy