Skip to content

Instantly share code, notes, and snippets.

@amancevice
Last active February 20, 2022 13:40
Show Gist options
  • Save amancevice/51fed600e10d3af1ea6d76aad0043d48 to your computer and use it in GitHub Desktop.
Save amancevice/51fed600e10d3af1ea6d76aad0043d48 to your computer and use it in GitHub Desktop.
Source .env file into new shell
#!/bin/sh
function dotenv() {
dotfile="${1:-.env}"
# Error if file does not exist
if ! [ -f $dotfile ] ; then
echo "$dotfile: No such file or directory" >&2
return 1
fi
# Log
tput setaf 209 ; printf 'source ' >&2
tput setaf 69 ; printf "$dotfile" >&2
tput sgr0 ; echo >&2
# Open new shell
$SHELL -c "set -o allexport ; source $dotfile ; set +o allexport ; $SHELL"
}
dotenv "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment