Last active
December 26, 2022 16:30
-
-
Save jweyrich/f7adcf1f5632b087ab8ebaf195c4e5fb to your computer and use it in GitHub Desktop.
Load environment variables from a dotenv file before running an executable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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