Skip to content

Instantly share code, notes, and snippets.

@devzom
Created March 16, 2022 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devzom/052f9806f3dd61d62fa71d1f28c85ae7 to your computer and use it in GitHub Desktop.
Save devzom/052f9806f3dd61d62fa71d1f28c85ae7 to your computer and use it in GitHub Desktop.
bash: Read .env file variable in Bash
# this will skip search for lines with variable starting with # (hashtag) ex: # VAR1 as its commented out
# Ex: we have .env file with variables:
# #VAR1=123
# VAR2=xyz
# NPM_TOKEN=12312323fdf21313
export "$(grep -vE "^(#.*|\s*)$" .env)"
# didn't respond VAR1 as it contain #, so it's commented out.
# responds with single value ex NPM_TOKEN:
echo $NPM_TOKEN # just print the single variable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment