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?
To set an environment variable, use the command " export varname=value ", which sets the variable and exports it to the global environment (available to other processes). Enclosed the value with double quotes if it contains spaces. To set a local variable, use the command " varname =value " (or " set varname =value ").
When you install SAP Data Services on UNIX or Linux platforms, the Job Server requires that you set certain environment variables. To set up these variables, run the script in al_env.sh. Points to the default Data Services installation directory, set by the installer. Includes the $LINK_DIR path for compatibility.
Simply put, environment variables are a set of dynamic named values stored within the system that is used by applications. These variables allow you to customize how specific applications and services behave with the system. Each variable contains a name and an associated value.
You can set your own variables at the command line per session, or make them permanent by placing them into the ~/. bashrc file, ~/. profile , or whichever startup file you use for your default shell. On the command line, enter your environment variable and its value as you did earlier when changing the PATH variable.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With