how to pass an environment variable to a shell command that I execute using Kernel#system
et al?
say, I want to run
%x{git checkout -f}
but this command relies on the environment variable $GIT_WORK_TREE
. how do I set it?
You should be able to set the variable in Ruby's ENV
hash prior to calling the sub-shell:
ENV['GIT_WORK_TREE'] = 'foo'
`echo $GIT_WORK_TREE`
should return "foo".
See the ENV[]=
documentation for more information.
[1] (pry) main: 0> ENV['GIT_WORK_TREE'] = 'foo' "foo" [2] (pry) main: 0> `echo $GIT_WORK_TREE` "foo\n"
You can use Process.spawn
to set the environment:
spawn({'GIT_WORK_TREE' => '/foo/bar'}, "git checkout -f")