Is it possible to load an .env file when accessing the directory they are in and unload these variables when changing to another directory?

2 Answers

Yes with bash.. This will change directory to the specified directory as normal and if a .env file exists there it will source it. If you add this snippet to your users .bashrc and source it.

function cd() { new_directory="$*"; if [ $# -eq 0 ]; then new_directory=${HOME}; fi; builtin cd "${new_directory}" if [ -f .env ]; then source .env fi } 
0

If you want load .env while opening a new tab

function loadenv() { if [ -f .env ]; then source .env fi } loadenv function cd() { builtin cd $@ loadenv } 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy