w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How can I import environment variables from a shell script into the current Ruby environment?

All shell invocations in Ruby run in a subprocess, whether you use system() or backticks or Process or any other mechanism to execute them. It is not possible for a subprocess like this to effect the current Ruby process.

If you want to source a shell script prior to executing some Ruby code, you can create a wrapper:

#!/bin/sh

source foo.sh
ruby some_ruby_file.rb

If you really want to, you can try to parse out variable exports from the shell script and then set Ruby's ENV hash directly, but that's almost certainly a bad idea. It'd be hard to write, error-prone, and unmaintainable.

Either use a wrapper as above, or come up with a different way to save your environment config, such as a YAML file or some other conventional configuration solution.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.