📜  dotenv - Shell-Bash (1)

📅  最后修改于: 2023-12-03 14:40:51.919000             🧑  作者: Mango

Dotenv - Shell-Bash

Introduction

Dotenv is a tool used to load environment variables from a .env file into a shell script. This makes it easier to manage environment variables for a project that has multiple dependencies and needs to run across different environments.

Features
  • Loads environment variables from a .env file into a shell script.
  • Can be used with any shell script, including Bash and Zsh.
  • Supports export command to make variables available to child processes.
  • Supports comments within the .env file.
  • Can be used alongside other tools like Docker and Kubernetes to manage environment variables.
Installation

To install Dotenv, you can use any package manager that supports Bash or Zsh. Here is an example using Homebrew:

brew install dotenv

You can also clone the GitHub repository and install manually:

git clone https://github.com/vlucas/phpdotenv.git
cd phpdotenv
make
sudo make install
Usage

To use Dotenv, create a .env file in your project directory containing your environment variables, each on a new line in the format KEY=VALUE. For example:

DB_HOST=localhost
DB_NAME=mydatabase
DB_USER=myusername
DB_PASSWORD=mypassword

In your shell script, load the .env file using the dotenv command:

#!/usr/bin/env bash

# Load environment variables from .env file
dotenv

# Use environment variables in script
echo "Connecting to ${DB_NAME} database on ${DB_HOST}..."
Conclusion

Dotenv is a powerful tool that simplifies the management of environment variables in shell scripts. It allows you to keep your variables in a separate file, making it easy to switch between different environments and manage dependencies. Try it out for your next project!