Reputation: 15329
I'm using the oh-my-zsh dotenv plugin, which loads .env
file, if it exists, whenever I cd
into a project directory.
I know it works because I can see my custom environment vars set when I run the set
cmd.
I can can also echo my custom environment variable from command line:
$ echo $FOO
'foo'
However, I cannot access this environment variable via the env
command or Ruby:
$ irb
2.4.1 :001 > ENV['FOO']
nil
How can I make sure environment variables loaded from my .env
are accessible from Ruby?
Upvotes: 1
Views: 1536
Reputation: 18389
Contrary to what is stated in the documentation of dotenv
, you actually need to use the export
keyword within the .env
file in order to make the parameters available to the environment, e.g.
export FOO=foo
The only exception would be, if the parameter was already an environment variable. For example if it had been exported in ~/.zshrc
or if it was already part of the environment zsh
got when it started (e.g. PATH
or HOME
).
All dotenv
does is automatically sourcing any .env
file when changing into a directory. There is no additional "magic". That means .env
needs to be a valid zsh
script and its content is run in the context of the current shell session (essentially as if you typed it manually).
It also means that the usual rules apply. That is, just settings parameters makes them available to the current shell context only. In order to make them available as environment variables they need to be exported (either before, during or after being set). So unless a parameter has already been exported before, export
is not really "optional" in .env
.
Upvotes: 3