Posts

Showing posts with the label Dotenv

How to set dotenv variables in bash shell when changing directory?

 To set environment variables from a `.env` file when changing directories in a Bash shell, you can use the `dotenv` tool or manually source the `.env` file. Here's how you can do it: 1. **Using dotenv Command** (Recommended):    The `dotenv` command is a tool designed for loading environment variables from a `.env` file. You can install it using Node.js and `npm`:    ```bash    npm install -g dotenv-cli    ```    Then, create a `.env` file in the directory where you want to load environment variables. The `.env` file should contain your environment variables in the format `VAR_NAME=VAR_VALUE`.    Now, you can use the `dotenv` command in your Bash script to load the variables:    ```bash    cd /path/to/your/directory    dotenv    ```    This will load the variables from the `.env` file in the current directory. 2. **Manually Sourcing the .env File**:    If you don't want to use `dotenv` and prefer a manual approach, you can source the `.env` file using the `source` or `.` comma