Load a dotenv file with bash

Jan 29, 2021
# Create the .env file.
cat .env << EOF
# Comment about variable A
ENV_VAR_A=Foo

ENV_VAR_B=Bar
EOF

# Export the contents as environment variables excluding comments and empty
# lines.
export $( \
  cat .env \
  | awk '!/(^#)|(^$)/ { print }' \
  | sed 's/#.*$//' | xargs \
)

echo $ENV_VAR_A
# Foo
echo $ENV_VAR_B
# Bar