Oct 12, 2015

Set environment variable when "cd"ing to the directory

I love automation! It saves a lot of time and helps to avoid mistakes.

When I work on projects, I sometimes need to set environment variables in the shell when I cd into a directory in the terminal. I ended up with a shell function that replaces the standard cd command. It checks if there is a .env file in the current directory and executes it on entering. When you leave the directory, if checks if the file .env_off exists and calls that. That file can contain normal shell commands, not just environment variables.

Do not forget to export variables in the .env file!

The script is here.

No comments:

Post a Comment