Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

need to set environment var for a back-ticks command

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?

like image 608
akonsu Avatar asked Apr 21 '13 03:04

akonsu


People also ask

What is the command to set the environment variable?

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 ").

How do I set an environment variable in SAP Linux?

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.

What is the _ environment variable in Linux?

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.

Where should I put environment variables in Linux?

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.


2 Answers

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"
like image 200
the Tin Man Avatar answered Oct 21 '22 02:10

the Tin Man


You can use Process.spawn to set the environment:

spawn({'GIT_WORK_TREE' => '/foo/bar'}, "git checkout -f")
like image 29
Mark Rushakoff Avatar answered Oct 21 '22 02:10

Mark Rushakoff