- 
      
- 
        Save JeyDi/026b34ddcf292953469e7ceca65052f1 to your computer and use it in GitHub Desktop. 
    Load environment variables from dotenv / .env file in Bash
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | if [ ! -f .env ] | |
| then | |
| export $(cat .env | xargs) | |
| fi | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Update
VSCode Python extension automatically loads the
.envfile in your project folder.Better to use something like that
[ ! -f .env ] || export $(grep -v '^#' .env | xargs)Or you can also use without xargs that it's easy:
export "$(grep -vE "^(#.*|\s*)$" .env)"Sometime this expression ca cause problems when you have complex stuff, so it's better to use a more complex regexp
source <(cat development.env | sed -e '/^#/d;/^\s*$/d' -e "s/'/'\\\''/g" -e "s/=\(.*\)/='\1'/g")In Python, you can also use do:
print(sorted(os.environ.items()))to print the variables.A possible function implementation for this: