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)

eval "$(direnv hook bash)"

Create .envrc file

And put all variables you would like to have accesible in this (and child) folder(s):

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

direnv allow

And whenever you enter this dir you should see:

user@my-pc:~/IdeaProjects$ cd terraform/
direnv: loading .envrc
direnv: export +AWS_ACCESS_KEY_ID +AWS_SECRET_ACCESS_KEY

and also whenever you leave

user@my-pc:~/IdeaProjects/terraform$ cd ..
direnv: unloading

Full docs: direnv.net

Leave a Reply

Your email address will not be published. Required fields are marked *