Skip to content

Instantly share code, notes, and snippets.

@jweyrich
Last active December 26, 2022 16:30
Show Gist options
  • Save jweyrich/f7adcf1f5632b087ab8ebaf195c4e5fb to your computer and use it in GitHub Desktop.
Save jweyrich/f7adcf1f5632b087ab8ebaf195c4e5fb to your computer and use it in GitHub Desktop.
Load environment variables from a dotenv file before running an executable
#!/bin/sh
#
# Load environment variables from a dotenv file before running an executable
# Usage: dotenv-exec <.env> <executable> [executable-parameters]
#
usage() {
echo "Usage: $(basename "$0") <.env> <executable> [executable-parameters]" 1>&2
exit 1
}
if [ -z "$1" ]; then
usage
fi
if [ ! -f "$1" ]; then
echo "File $1 not found" 1>&2
usage
fi
# Original source from https://stackoverflow.com/a/73589795/298054
set -a # equivalent to: set -o allexport
. "$1"
set +a # equivalent to: set +o allexport
shift # Remove the .env argument
exec $@ # Run the executable with all original parameters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment