# Setting Windows Enviroment in Ruby Script

**URL:** <https://www.ruby-forum.com/t/setting-windows-enviroment-in-ruby-script/180375>\
**Category:** Ruby\
**Created:** [December 18, 2009, 6:24pm UTC](https://www.ruby-forum.com/t/setting-windows-enviroment-in-ruby-script/180375 "2009-12-18T18:24:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [December 18, 2009, 6:24pm UTC](https://www.ruby-forum.com/t/setting-windows-enviroment-in-ruby-script/180375/1 "2009-12-18T18:24:45Z")

</div>

I need to set the following windows environment variable at the  
beginning of my script:

```
set http_proxy=http://10.1.1.1:8080

```

This works from the command line if I run this command from the CLI  
right before I run my script but when I try to shell out and the run  
command like this from my ruby script:

```
`set http_proxy=http://10.1.1.1:8080`

```

The environmental variable never gets set.

What is the best way to set an environmental variable in Windows from  
ruby?

thanks

jack

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [December 18, 2009, 7:50pm UTC](https://www.ruby-forum.com/t/setting-windows-enviroment-in-ruby-script/180375/2 "2009-12-18T19:50:41Z")

</div>

On 18.12.2009 18:24, jackster the jackle wrote:

> The environmental variable never gets set.
> 
> What is the best way to set an environmental variable in Windows from  
> ruby?

“set” only sets a variable for the process that invokes “set”:

Quoth Microsoft:  
“Use the set command to create, change, delete, or display environment  
variables. The set command alters variables in the current shell  
environment only.”[0]

[http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx?mfr=true](http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx?mfr=true)

So, your variable gets set, but since the cmd shell expires, it takes  
the environment variable with it.

You could try setting the variable in the Registry, but I advise against  
it (not a robust solution for something that is intended to be  
temporary, and it needs administration privileges).

You could define a Constant in Ruby, though, if only your Ruby script  
needs it, like so:  
HTTP\_PROXY = your\_proxy\_ip\_address

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [December 18, 2009, 8:47pm UTC](https://www.ruby-forum.com/t/setting-windows-enviroment-in-ruby-script/180375/3 "2009-12-18T20:47:27Z")

</div>

On Dec 18, 2:24 pm, jackster the jackle [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I need to set the following windows environment variable at the  
> beginning of my script:
> 
> ```
> set http_proxy=http://10.1.1.1:8080
> 
> ```

ENV[‘http\_proxy’] = “[http://10.1.1.1:8080](http://10.1.1.1:8080)”

That will set the environment variable for your current process and  
also will be transfered to the child process spawned by it.

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [December 18, 2009, 9:55pm UTC](https://www.ruby-forum.com/t/setting-windows-enviroment-in-ruby-script/180375/4 "2009-12-18T21:55:14Z")

</div>

Thanks alot for the help Luis…that worked perfectly for what I needed.

jack

Luis L. wrote:

> On Dec 18, 2:24ï¿½pm, jackster the jackle [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I need to set the following windows environment variable at the  
> > beginning of my script:
> > 
> > ï¿½ ï¿½ set http\_proxy=[http://10.1.1.1:8080](http://10.1.1.1:8080)
> 
> ENV[‘http\_proxy’] = “[http://10.1.1.1:8080](http://10.1.1.1:8080)”
> 
> That will set the environment variable for your current process and  
> also will be transfered to the child process spawned by it.
