1. What does it do?
When working on software projects, it’s easier to handle the environment varialbes with .env
file which usually contains any environment varialbes for the application to function as intended.
For example:
AWS_S3_TOKEN=d84a83539134f28f412c652b09f9f98eff96c9a
SECRET_KEY=7c6c72d959416d5aa368a409362ec6e2ac90d7f
MONGO_URI=mongodb://127.0.0.1:27017
PORT=3001
There are many packages to allow this to be loaded in the application but I needed this in the shell and look for some good solutions
2. omz with dotenv plugin
I found dotenv plugin for ohmyzsh that can be used with zsh and ohmyzsh environment.
3. How to use
- Assuming you’re using zsh and installed ohmyzsh
- open .zshrc file
- find plugins section and add “dotenv”
# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(
git
dotenv # <= add this
)
- Once this is done, when you go to the directory with the .env file in it. It will ask if you want the .env file to be loaded in or not.
cd my_software_project
dotenv: found '.env' file. Source it? ([Y]es/[n]o/[a]lways/n[e]ver) Y
- If you want to allow it permanently, you can do [a]lways. The file will be stored in
$ZSH_CACHE_DIR/dotenv-allowed.list
and$ZSH_CACHE_DIR/dotenv-disallowed.list
. The default for $ZSH_CACHE_DIR is~/.oh-my-zsh/cache
~ cd .oh-my-zsh/cache
~ ls
completions dotenv-allowed.list dotenv-disallowed.list grep-alias
~ cat dotenv-allowed.list
/Users/me/my_software_project