8

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?

Community
  • 1
  • 1
Rogach
  • 26,050
  • 21
  • 93
  • 172
  • 1
    This sound backwards to me. What are you trying to achieve? – Jens Tinfors Feb 19 '12 at 17:54
  • @JensTinfors i just used this in a ruby script which makes some shell commands, one of which expects a specific environment variable to be set. If i was doing it in a bash script i would use `export`, but this won't work via a ruby shell command (backticks or system). It seems to work fine using ENV like the answer say, so this was very valuable to me. – Max Williams Mar 02 '16 at 14:24
  • @MaxWilliams Its just that the environment variables you set will only be there for the current process. By backwards I mean that environment belongs to the shell, the ruby process is a subprocess of the shell, see what I mean? :) – Jens Tinfors Mar 15 '16 at 16:33

3 Answers3

11

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

ENV['SOME_VAR'] = 'some_value'
ruakh
  • 175,680
  • 26
  • 273
  • 307
  • 1
    NB: this will set the environment variable for the current Ruby process as well as any sub-processes (eg: that you create with `system` or backticks). – Kyle Burton Feb 19 '12 at 18:00
  • @KyleBurton: Yes, absolutely. I'm assuming that's exactly what the OP wants, but I could be wrong. – ruakh Feb 19 '12 at 18:02
2

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
Irfy
  • 9,323
  • 1
  • 45
  • 67
  • 1
    I really don't think `source ./my_script.rb` can be made to work. Think about it -- for what you're saying to be true, the Ruby script would have to run in the same process as the existing Bash shell, and then, when the Ruby script ended, the process would have to revert to being a Bash shell. – ruakh Feb 19 '12 at 17:55
  • What the shebang should look like in this case? – Sergio Tulentsev Feb 19 '12 at 17:56
  • You can 'source' the output of a program with bash like this: `source <(./my_script.rb)` – Kyle Burton Feb 19 '12 at 17:59
  • @KyleBurton: hm, tried just now, didn't work. I'm on mac osx. – Sergio Tulentsev Feb 19 '12 at 18:02
  • Hmm, yes, that source thing was a long shot, my mistake. – Irfy Feb 19 '12 at 18:09
  • @sergio-tulentsev the output from `my_script.rb` has to be valid bash syntax for it to work - what did you attempt to source? – Kyle Burton Feb 19 '12 at 18:50
  • @KyleBurton It really doesn't work under Mac, just tried it myself. Under Linux the exact same script works like a charm. Weird... – Irfy Feb 19 '12 at 19:07
  • On Linux I have `GNU bash 4.1.5(1)-release (x86_64-pc-linux-gnu)` and on Mac I have `GNU bash, version 3.2.17(1)-release (i386-apple-darwin9.0)`. Maybe that's the culprit – Irfy Feb 19 '12 at 19:08
  • @KyleBurton: i tried `source <(cat env.txt)` where env.txt contains valid `export` statement. – Sergio Tulentsev Feb 19 '12 at 19:21
2

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.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367