Takahiro Kudo
Posted on December 19, 2020
Sample codes to load environment variables from .env file in sh.
#!/usr/bin/env sh
# Load variables from .env file.
export $(cat ./.env | grep -v ^# | xargs) >/dev/null
# Refer to variables.
echo "KEY_IN_ENV=${KEY_IN_ENV}"
Here is test code, which creates test .env and echo it.
#!/usr/bin/env sh
TEST_KEY1="This value will be replaced."
# Create test .env
cat <<EOF > .env
TEST_KEY1=VALUE1
TEST_KEY2=VALUE2
EOF
# Load .env
export $(cat ./.env | grep -v ^# | xargs) >/dev/null
# Check
echo "TEST_KEY1=${TEST_KEY1}"
echo "TEST_KEY2=${TEST_KEY2}"
💖 💪 🙅 🚩
Takahiro Kudo
Posted on December 19, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.