From d9d559c327f87659fe54df8a176f652b8398c771 Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Sat, 8 Feb 2020 14:23:18 -0600 Subject: [PATCH] functions: dotenv: export variables from .env Tools (such as VSCode) can read and use environment variables from a file in the current working directory named `.env`. Unfortunately, these tools are not exactly compatible with shell syntax, as they do not correctly process lines starting with `export`. The `dotenv` function will read a `.env` file from the current working directory and then export all of the variables it defines. This allows the `.env` file to define the variables without "exporting" them, making them compatible with VSCode et al, but still allowing the same environment variables to be set in the shell and its child processes. --- functions/dotenv | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 functions/dotenv diff --git a/functions/dotenv b/functions/dotenv new file mode 100644 index 0000000..4ce0354 --- /dev/null +++ b/functions/dotenv @@ -0,0 +1,10 @@ +# vim: set ft=zsh sw=4 ts=4 sts=4 et : + +function dotenv() { + if [ -f .env ]; then + . ./.env || return $? + eval $(awk -F= '{print "export",$1}' .env) + fi +} + +dotenv