Moshe
Moshe

Reputation: 493

file containing env vars not loading

I am trying to have my system automatically load a file containing some environment vars to use in my rails project instead of just exporting them to the shell but i just cant seem to get it working.

i'm using mac osx 10.6

these are steps i followed.

nano .bash_profile

export PATH=Users/Moshe/env_vars:$PATH

touch env_vars

nano env_vars

DEV_DB=TestDB
export DEV_DB

DEV_DB_HOST=12.34.56.78
export DEV_DB_HOST

exit

I reopen the shell and

printenv

but i don't see the vars i set

however i do see it Users/Moshe/env_vars in the path when i echo $PATH

i have tried looking at this answer but i cant seem to see what im doing wrong

Setting environment variables in OS X?

Upvotes: 0

Views: 2004

Answers (2)

Малъ Скрылевъ
Малъ Скрылевъ

Reputation: 16515

Don't use exit keyword. Either insert the code into .bash_profile, or store it into the specific file, for example your_script.sh, and call from .bash_profile as follows:

. your_script.sh

Upvotes: 1

Ash Wilson
Ash Wilson

Reputation: 24478

Environment variables aren't read from files found on your $PATH; your $PATH is searched to find executable files when you type a command in the shell.

To load your variables, you'll need to "source" the file instead:

# .bash_profile
source /Users/Moche/env_vars

(Notice that you also need the initial / to make the filename absolute.)

Also, since you're using Ruby, you might be interested in using the dotenv gem. dotenv lets you specify environment variables in a .env file in the root of your Rails project and automatically load them on startup. This keeps the variables you need for each project close to the code and makes your project a little more portable (you can use this to deploy on a service like Heroku, for example). Just make sure you don't commit .env into your version control if it has any sensitive information like passwords in it!

Upvotes: 1

Related Questions