1

I have a Ruby script (say, genrc.rb) that generates a custom rc file (say, ~/.custom_rc). How can I source this file in to the current shell by running the ruby script?

$ ruby genrc.rb
$ # commands from the ~/.custom_rc should be available here

1 Answer 1

3

A Ruby script (or any program, for that matter) cannot alter the shell that started it, so you’ll either have to run

source ~/.custom_rc

after running the script, or have the script also output the contents of the file to stdout, in which case you could do

source <(ruby genrc.rb)

or

eval "$(ruby genrc.rb)"

The script could also instead output the path to the written file, and you could do:

source $(ruby genrc.rb)
Sign up to request clarification or add additional context in comments.

2 Comments

That's little unfortunate. But your suggested solutions will work, too. Thanks.
@codefx It’s really a good thing, though, keeps programs from unexpectedly changing your environment. Only shell built-ins (e.g. eval & source) have that power, everything else is a forked process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.