Permanently exporting environment variables using direnv (Linux)
PROBLEM: I use some variables (credentials) so often, but I don’t like exporting them each time
SOLUTION: Start using direnv!
Direnv should be already present in your system
You can check the same on the direnv website (here), if is not, you can always build it from binary.
Hook direnv into shell
Add this line on the end of your .bashrc file (if you are using bash)
1 |
eval "$(direnv hook bash)" |
Create .envrc file
And put all variables you would like to have accesible in this (and child) folder(s):
1 2 |
export AWS_ACCESS_KEY_ID=123456AB654321 export AWS_SECRET_ACCESS_KEY=AB123456AB987654 |
Allow direnv
Just run this command inside the dir where you created .envrc file
1 |
direnv allow |
And whenever you enter this dir you should see:
1 2 3 |
user@my-pc:~/IdeaProjects$ cd terraform/ direnv: loading .envrc direnv: export +AWS_ACCESS_KEY_ID +AWS_SECRET_ACCESS_KEY |
and also whenever you leave
1 2 |
user@my-pc:~/IdeaProjects/terraform$ cd .. direnv: unloading |
Full docs: direnv.net