FlyingTeller
FlyingTeller

Reputation: 20526

Accesing environment variables with $ and ENV in ruby

I am trying to acces an environment variable set in ~/.bashrc

In ~/.bashrc I have set

export testdata = <some path>

without the <> of course. I may add that I also tried adding

testdata=<some path>

to /etc/environment

When I am still in the shell I can do

echo $testdata

which gives as result When in my script I can do

puts ENV['testdata']

which will print So far so good. However I another script that was not written by me where there is a line like

if $testdata then
#some code
end

which is supposed to just execute the code when the environment varaible is set. However this code is not working for me. Only when I replace $ with ENV[] the code is correclty executed.

Upvotes: 2

Views: 7587

Answers (1)

Aleksey Shein
Aleksey Shein

Reputation: 7482

This is expected behavior. $var is a global variable in Ruby, and not an environment variable. To access environment variable, as you said, you need to use ENV['var'].

Upvotes: 3

Related Questions