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 `.` command in Bash:
```bash
cd /path/to/your/directory
source .env
```
or
```bash
cd /path/to/your/directory
. .env
```
Make sure the `.env` file is in the specified directory and contains the environment variables you want to load.
Using the `dotenv` command is a cleaner and more standardized way to load environment variables from a `.env` file, and it's especially useful if you work with Node.js projects. However, manually sourcing the file with `source` or `.` also works if you prefer to use standard Bash commands.