How to Source Environment Variables for a Command Shell in a Ruby Script

How do I source environment variables for a command shell in a Ruby script?

Do this:

$ source whatever.sh
$ set > variables.txt

And then in Ruby:

File.readlines("variables.txt").each do |line|
values = line.split("=")
ENV[values[0]] = values[1]
end

After you've ran this, your environment should be good to go.

Source shell script into environment within a ruby script

The reason this isn't working for you is b/c ruby runs its system commands in separate shells. So when one system command finishes, the shell that had sourced your file closes, and any environment variables set in that shell are forgotten.

If you don't know the name of the sourced file until runtime, then Roboprog's answer is a good approach. However, if you know the name of the sourced file ahead of time, you can do a quick hack with the hashbang line.

% echo sourcer.rb
#!/usr/bin/env ruby
exec "csh -c 'source #{ARGV[0]} && /usr/bin/env ruby #{ARGV[1]}'"
% echo my-script.rb
#!/usr/bin/env ruby sourcer.rb /path/to/file/I/want/to/source.csh
puts "HAPPYTIMES = #{ENV['HAPPYTIMES']}"
% ./my-script.rb
HAPPYTIMES = True

All of these will only help you use the set enviroment variables in your ruby script, not set them in your shell (since they're forgotten as soon as the ruby process completes). For that, you're stuck with the source command.

Environment variables in ruby script on Linux

The .env file is loaded from the root of your project. Specifically in your case from the current directory. Try moving into the directory containing the .env file before executing the script.

cd /path/to/ && ./myscript.rb

Update:

You can load the .env file from the script path without moving there:

config = File.absolute_path(File.join(File.expand_path(__FILE__), '..', '.env'))
Dotenv.load(config)

Ruby system call with environment variables in windows

Ruby's system() invokes whatever the host's default shell is, so you need to speak that shell's language.

The default shell on Windows is cmd.exe, where environment variable FOO must be referenced as %FOO% in order to be expanded.

Thus, your code should be:

cmd = 'echo %FOO%'
system({'FOO' => '123'}, cmd)

Exporting an Environment Variable in Ruby

Simple answer: You can't.

Longer answer: You can't, unless the operating environment provides hooks to do so. Most do not. The best you can usually do is print out the assignments you want done and have the parent execute them.

Is it possible to export environment property from ruby script?

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

ENV['SOME_VAR'] = 'some_value'

How to execute setenv command inside ruby code

Environment variables belong to a process and are passed to their children. All of your lines above create a child shell process. The environment variable is changed in the child shell process, not its parent Ruby process.

Instead, use the ENV class.

ENV['HEMANT2'] = 'hi'

This will set the HEMANT2 environment variable in your Ruby process. But it will not change the environment variables in your shell.

$ cat ~/tmp/test.rb
ENV['HEMANT2'] = 'hi'
$ ruby ~/tmp/test.rb
$ echo $HEMANT2

$

Again, because environment variables belong to a process and are inherited by their children. ruby ~/tmp/test.rb forks a child process of your shell. It cannot set its parent's environment variables.


You can do it with a shell script, but only if you source it. source does not create a child process, it runs the commands in your shell.

$ cat test.sh
#!/bin/sh

HEMANT2=hi

$ sh test.sh
$ echo $HEMANT2

$ source test.sh
$ echo $HEMANT2
hi

This only works with shell scripts written for your shell.



Related Topics



Leave a reply



Submit