I just faced an amazing tool, called Direnv, which I immediately put in my toolbox. The name reveals the intention - it manages environment variables per folder. It knows how to hook into different shells and load variables from a configuration file just per that particular folder.
Let's install it:
brew install direnv
Then attach to your shell of choice:
eval "$(direnv hook bash)"
Now we are good to create a configuration file, where all the variables per directory/project will be stored:
echo export SENDGRID_KEY=SOME_KEY > .envrc
The format of the file is quite simple. You just export key/value pair. That's it:
To load the variables run (usually when you edit or add new):
To summarize there is the following workflow:
- Create .envrc file per project/directory
- Fill .envrc with variable
- Use the variables in the scope of the directory