# Setting environment variables

**URL:** <https://rubytalk.org/t/setting-environment-variables/33187>\
**Category:** ruby-talk\
**Created:** [24 November 2006 21:35 UTC](https://rubytalk.org/t/setting-environment-variables/33187 "2006-11-24T21:35:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Collier](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@David\_Collier](https://rubytalk.org/u/David_Collier)\
**Post date:** [24 November 2006 21:35 UTC](https://rubytalk.org/t/setting-environment-variables/33187/1 "2006-11-24T21:35:59Z")

</div>

hi -

i have a ruby command line script, and i want to set/export an  
environment variable thats available to other various shell scripts  
that are called by this ruby...

can someone tell me how to do this?

i thot this might work, but not...

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ENV[' ENV['RSWF\_C'] = 1 # use c bindings'] = 1

if i use (in ruby app:)  
&nbsp;&nbsp;&nbsp;system(" RSWF\_C=1; export RSWF\_C" )

is this available to all other scripts called from this master ruby script?

thanks for any help, I'm a bit lost!

/dc

> **···**
>
> -------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;David "DC" Collier  
> [mailto:dc@pikkle.com](mailto:dc@pikkle.com)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+81 (0)80 6521 9559  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;skype: callto://d3ntaku  
> -------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Pikkle 株式会社  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[http://www.pikkle.com](http://www.pikkle.com)  
> -------------------------------------------

---

<div class="post-metadata">

**Author:** ![Paul\_Lutus](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@Paul\_Lutus](https://rubytalk.org/u/Paul_Lutus)\
**Post date:** [24 November 2006 23:00 UTC](https://rubytalk.org/t/setting-environment-variables/33187/2 "2006-11-24T23:00:15Z")

</div>

dc wrote:

> hi -
> 
> i have a ruby command line script, and i want to set/export an  
> environment variable thats available to other various shell scripts  
> that are called by this ruby...

You can't do that. Each call to "system()" launches a new shell interpreter,  
and they don't share environments.

> can someone tell me how to do this?

You cannot do this.

> i thot this might work, but not...
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ENV[' ENV['RSWF\_C'] = 1 # use c bindings'] = 1
> 
> if i use (in ruby app:)  
> &nbsp;&nbsp;&nbsp;system(" RSWF\_C=1; export RSWF\_C" )
> 
> is this available to all other scripts called from this master ruby  
> script?

No.

Now, please tell us what problem you are trying to solve, rather than  
evaluate this approach to solving it.

> **···**
>
> --  
> Paul Lutus  
> [http://www.arachnoid.com](http://www.arachnoid.com)

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [24 November 2006 23:13 UTC](https://rubytalk.org/t/setting-environment-variables/33187/3 "2006-11-24T23:13:37Z")

</div>

why not?

&nbsp;&nbsp;&nbsp;harp:~ \> ruby -e' ENV["VAR"] = 42.to\_s; system "env|grep VAR" '  
&nbsp;&nbsp;&nbsp;VAR=42

the environment is definitely shared. did i mis-understand?

kind regards.

-a

> **···**
>
> On Sat, 25 Nov 2006, Paul Lutus wrote:
> 
> > dc wrote:
> > 
> > > hi -
> > > 
> > > i have a ruby command line script, and i want to set/export an  
> > > environment variable thats available to other various shell scripts  
> > > that are called by this ruby...
> > 
> > You can't do that. Each call to "system()" launches a new shell interpreter,  
> > and they don't share environments.
> > 
> > > can someone tell me how to do this?
> > 
> > You cannot do this.
> 
> --  
> if you want others to be happy, practice compassion.  
> if you want to be happy, practice compassion. -- the dalai lama

---

<div class="post-metadata">

**Author:** ![Paul\_Lutus](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@Paul\_Lutus](https://rubytalk.org/u/Paul_Lutus)\
**Post date:** [24 November 2006 23:25 UTC](https://rubytalk.org/t/setting-environment-variables/33187/4 "2006-11-24T23:25:13Z")

</div>

/ ...

> > You cannot do this.
> 
> why not?
> 
> &nbsp;&nbsp;&nbsp;harp:~ \> ruby -e' ENV["VAR"] = 42.to\_s; system "env|grep VAR" '  
> &nbsp;&nbsp;&nbsp;VAR=42
> 
> the environment is definitely shared. did i mis-understand?

My reply was based on his diagrammed effort to use one system call to refer  
to another system call:

> > if i use (in ruby app:)  
> > system(" RSWF\_C=1; export RSWF\_C" )

> > is this available to all other scripts called from this  
> > master ruby script?

The answer is obvious, and I provided it.

If instead his goal can be met by setting ENV variables within Ruby, then  
that solves the problem. But before suggesting this, I asked that the OP  
first explain what he is trying to do.

> **···**
>
> ara.t.howard@noaa.gov wrote:
> 
> --  
> Paul Lutus  
> [http://www.arachnoid.com](http://www.arachnoid.com)

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [25 November 2006 00:18 UTC](https://rubytalk.org/t/setting-environment-variables/33187/5 "2006-11-25T00:18:33Z")

</div>

then we are both right - the OP can solve his problem, but not the way he  
intended.

regards.

-a

> **···**
>
> On Sat, 25 Nov 2006, Paul Lutus wrote:
> 
> > > > if i use (in ruby app:)  
> > > > system(" RSWF\_C=1; export RSWF\_C" )
> > 
> > > > is this available to all other scripts called from this  
> > > > master ruby script?
> > 
> > The answer is obvious, and I provided it.
> > 
> > If instead his goal can be met by setting ENV variables within Ruby, then  
> > that solves the problem. But before suggesting this, I asked that the OP  
> > first explain what he is trying to do.
> 
> --  
> if you want others to be happy, practice compassion.  
> if you want to be happy, practice compassion. -- the dalai lama

---

<div class="post-metadata">

**Author:** ![El\_Gato](https://avatars.discourse-cdn.com/v4/letter/e/71e660/32.png) [@El\_Gato](https://rubytalk.org/u/El_Gato)\
**Post date:** [25 November 2006 02:40 UTC](https://rubytalk.org/t/setting-environment-variables/33187/6 "2006-11-25T02:40:12Z")

</div>

Paul Lutus wrote:

> / ...
> 
> > > You cannot do this.
> > 
> > why not?
> > 
> > &nbsp;&nbsp;&nbsp;harp:~ \> ruby -e' ENV["VAR"] = 42.to\_s; system "env|grep VAR" '  
> > &nbsp;&nbsp;&nbsp;VAR=42
> > 
> > the environment is definitely shared. did i mis-understand?
> 
> My reply was based on his diagrammed effort to use one system call to  
> refer  
> to another system call:
> 
> > > if i use (in ruby app:)  
> > > system(" RSWF\_C=1; export RSWF\_C" )
> 
> > > is this available to all other scripts called from this  
> > > master ruby script?
> 
> The answer is obvious, and I provided it.
> 
> If instead his goal can be met by setting ENV variables within Ruby,  
> then  
> that solves the problem. But before suggesting this, I asked that the OP  
> first explain what he is trying to do.

Gee, I thought it was quite obvious that he was trying to export a shell  
variable. Was it not at least somewhat obvious to you? His first  
paragraph was relatively straightforward about that. Anyways, my point  
is that you probably could've just gone ahead and given him the answer  
-- it would've added a mere 20 chars (max) to your post and been at  
least 100 times more helpful. I dread the day when all of the Ruby hype  
turns this community into the elitist/hostile environment that exists  
for so many other languages.

> **···**
>
> > ara.t.howard@noaa.gov wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Christopher\_Aldridge](https://avatars.discourse-cdn.com/v4/letter/c/34f0e0/32.png) [@Christopher\_Aldridge](https://rubytalk.org/u/Christopher_Aldridge)\
**Post date:** [25 November 2006 02:55 UTC](https://rubytalk.org/t/setting-environment-variables/33187/7 "2006-11-25T02:55:41Z")

</div>

agreed.

Works on win32 also:

irb#1(main):011:0\> ENV['time'] = Time.now.to\_s and system('set|find "time"')  
time=Fri Nov 24 21:55:23 -0500 2006  
=\> true

> **···**
>
> On 11/24/06, El Gato \<wmwilson01@gmail.com\> wrote:
> 
> > Paul Lutus wrote:  
> > \> ara.t.howard@noaa.gov wrote:  
> > \>  
> > \> / ...  
> > \>  
> > \>\>\> You cannot do this.  
> > \>\>  
> > \>\> why not?  
> > \>\>  
> > \>\> harp:~ \> ruby -e' ENV["VAR"] = 42.to\_s; system "env|grep VAR" '  
> > \>\> VAR=42  
> > \>\>  
> > \>\> the environment is definitely shared. did i mis-understand?  
> > \>  
> > \> My reply was based on his diagrammed effort to use one system call to  
> > \> refer  
> > \> to another system call:  
> > \>  
> > \>\>\> if i use (in ruby app:)  
> > \>\>\> system(" RSWF\_C=1; export RSWF\_C" )  
> > \>  
> > \>\>\> is this available to all other scripts called from this  
> > \>\>\> master ruby script?  
> > \>  
> > \> The answer is obvious, and I provided it.  
> > \>  
> > \> If instead his goal can be met by setting ENV variables within Ruby,  
> > \> then  
> > \> that solves the problem. But before suggesting this, I asked that the OP  
> > \> first explain what he is trying to do.
> > 
> > Gee, I thought it was quite obvious that he was trying to export a shell  
> > variable. Was it not at least somewhat obvious to you? His first  
> > paragraph was relatively straightforward about that. Anyways, my point  
> > is that you probably could've just gone ahead and given him the answer  
> > -- it would've added a mere 20 chars (max) to your post and been at  
> > least 100 times more helpful. I dread the day when all of the Ruby hype  
> > turns this community into the elitist/hostile environment that exists  
> > for so many other languages.
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![David\_Collier](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@David\_Collier](https://rubytalk.org/u/David_Collier)\
**Post date:** [25 November 2006 04:27 UTC](https://rubytalk.org/t/setting-environment-variables/33187/8 "2006-11-25T04:27:56Z")

</div>

hi -

thanks for the help everyone.

> The answer is obvious, and I provided it.

indeed you did! you remind me of a very stoic colleague 🙂

the background is this:

we have a server written in ruby  
when it starts up it looks for some environment variables for which  
mode to run in (eg using custom c extensions, or just pure ruby)

I have a completely different ruby script (for benchmarking)

so, the benchmark script needs to communicate with the server, and the  
way things are setup now, this would be via environment vars.

so,

bench.rb \>\> sets ENV['FLAG']  
calls server.rb to start up (via a system() call currently)

server.rb \>\> on startup, looks for the FLAG env var and decides how to run.

I tried to do some research on what exactly ENV is, within ruby, but  
didnt get much out of google...its a bit too common of a word.

if i just call system() to set an env flag,  
then export it  
will it then be available to other processes?

any more info on ENV would also be helpful  
(or where to look - i find the ruby docs really awful)

> ri ENV

Nothing known about ENV

thanks for any help!

/dc

> **···**
>
> On 25/11/06, El Gato \<wmwilson01@gmail.com\> wrote:
> 
> > Paul Lutus wrote:  
> > \> ara.t.howard@noaa.gov wrote:  
> > \>  
> > \> / ...  
> > \>  
> > \>\>\> You cannot do this.  
> > \>\>  
> > \>\> why not?  
> > \>\>  
> > \>\> harp:~ \> ruby -e' ENV["VAR"] = 42.to\_s; system "env|grep VAR" '  
> > \>\> VAR=42  
> > \>\>  
> > \>\> the environment is definitely shared. did i mis-understand?  
> > \>  
> > \> My reply was based on his diagrammed effort to use one system call to  
> > \> refer  
> > \> to another system call:  
> > \>  
> > \>\>\> if i use (in ruby app:)  
> > \>\>\> system(" RSWF\_C=1; export RSWF\_C" )  
> > \>  
> > \>\>\> is this available to all other scripts called from this  
> > \>\>\> master ruby script?  
> > \>  
> > \> The answer is obvious, and I provided it.  
> > \>  
> > \> If instead his goal can be met by setting ENV variables within Ruby,  
> > \> then  
> > \> that solves the problem. But before suggesting this, I asked that the OP  
> > \> first explain what he is trying to do.
> > 
> > Gee, I thought it was quite obvious that he was trying to export a shell  
> > variable. Was it not at least somewhat obvious to you? His first  
> > paragraph was relatively straightforward about that. Anyways, my point  
> > is that you probably could've just gone ahead and given him the answer  
> > -- it would've added a mere 20 chars (max) to your post and been at  
> > least 100 times more helpful. I dread the day when all of the Ruby hype  
> > turns this community into the elitist/hostile environment that exists  
> > for so many other languages.
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> -------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;David "DC" Collier  
> [mailto:dc@pikkle](mailto:dc@pikkle).com  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+81 (0)80 6521 9559  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;skype: callto://d3ntaku  
> -------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Pikkle 株式会社  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[http://www.pikkle.com](http://www.pikkle.com)  
> -------------------------------------------

---

<div class="post-metadata">

**Author:** ![Paul\_Lutus](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@Paul\_Lutus](https://rubytalk.org/u/Paul_Lutus)\
**Post date:** [25 November 2006 06:50 UTC](https://rubytalk.org/t/setting-environment-variables/33187/9 "2006-11-25T06:50:07Z")

</div>

I agree that we are both right, but you are at least 10% more right than I  
am. 🙂

I should have read his post more carefully -- I didn't spend enough time on  
it.

> **···**
>
> ara.t.howard@noaa.gov wrote:
> 
> > On Sat, 25 Nov 2006, Paul Lutus wrote:
> > 
> > > > > if i use (in ruby app:)  
> > > > > system(" RSWF\_C=1; export RSWF\_C" )
> > > 
> > > > > is this available to all other scripts called from this  
> > > > > master ruby script?
> > > 
> > > The answer is obvious, and I provided it.
> > > 
> > > If instead his goal can be met by setting ENV variables within Ruby, then  
> > > that solves the problem. But before suggesting this, I asked that the OP  
> > > first explain what he is trying to do.
> > 
> > then we are both right - the OP can solve his problem, but not the way he  
> > intended.
> 
> --  
> Paul Lutus  
> [http://www.arachnoid.com](http://www.arachnoid.com)

---

<div class="post-metadata">

**Author:** ![Curt\_Sampson](https://avatars.discourse-cdn.com/v4/letter/c/e480ec/32.png) [@Curt\_Sampson](https://rubytalk.org/u/Curt_Sampson)\
**Post date:** [25 November 2006 04:38 UTC](https://rubytalk.org/t/setting-environment-variables/33187/10 "2006-11-25T04:38:11Z")

</div>

> I tried to do some research on what exactly ENV is, within ruby, but  
> didnt get much out of google...its a bit too common of a word.

Check Pixaxe. Even the on-line 1st edition should have a good discussion  
of ENV.

> if i just call system() to set an env flag,  
> then export it  
> will it then be available to other processes?

Under Unix, the environment is a set of keywords and associated values  
available to the current process. When you fork a new process (creating  
a child process), it inherits a COPY of the environment of the parent  
process.

system forks a new child process. So that child, if it sets an  
environment variable will have access to it, as will all of its  
children. The parent (your ruby script) will not be affected, nor will  
any other processes that are not children of the child that set the  
environment variable.

Probably best thing to do would be to find something somewhere that  
explains the Unix process model; once you understand that, simple  
reasoning will let you figure out what's going on and what you need to do.

Oh, and changing ENV does indeed change the environment of the current  
process:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ cat z  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system('echo before: $FOO')  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ENV['FOO'] = "now it's set"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system('echo after: $FOO')  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ ruby z  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;before:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;after: now it's set  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$

cjs

> **···**
>
> On Sat, 25 Nov 2006, dc wrote:  
> --  
> Curt Sampson \<cjs@cynic.net\> +81 90 7737 2974  
> &nbsp;&nbsp;&nbsp;The power of accurate observation is commonly called cynicism  
> &nbsp;&nbsp;&nbsp;by those who have not got it. --George Bernard Shaw
