BASH
Load .env File into Bash Environment
Learn how to securely load environment variables from a .env file into your Bash session or scripts, enabling easy configuration management for web projects.
#!/bin/bash
# Source environment variables from .env file
if [ -f .env ]; then
export $(grep -v '^#' .env | xargs)
echo "Environment variables loaded from .env"
else
echo ".env file not found. Skipping environment variable loading."
fi
# Example usage:
# echo "DATABASE_HOST: $DATABASE_HOST"
# echo "API_KEY: $API_KEY"
How it works: This script checks for the existence of a `.env` file. If found, it reads each line (excluding comments starting with `#`) and exports them as environment variables. This is a common pattern in web development to manage sensitive configuration data without hardcoding it directly into scripts or application code, making deployment and local development easier and more secure.