3

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.

FlyingTeller
  • 17,638
  • 3
  • 38
  • 53
  • 2
    Variables prefixed with $ are not environment variables in Ruby, they're global variables. Do `$testdata = ENV['testdata']` if you need that. – Aldehir Sep 14 '15 at 19:51

1 Answers1

3

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'].

Community
  • 1
  • 1
Alexey Shein
  • 7,342
  • 1
  • 25
  • 38