|
| 1 | +# @devmy/dotenv2shell - Load Environment Variables from .env File |
| 2 | + |
| 3 | +This TypeScript CLI lets you easily load environment variables from a `.env` file into your current shell session with [@dotenv-run/core](https://www.npmjs.com/package/@dotenv-run/core). It provides flexibility through various options and supports both zsh and bash. |
| 4 | + |
| 5 | +- ✅ Load environment variables from .env files |
| 6 | +- ✅ Load environment variables from .env.vault files |
| 7 | +- ✅ Expand environment variables API_URL=$API_BASE/users |
| 8 | +- ✅ Define environment variables for a specific environment (e.g. .env.production) |
| 9 | +- ✅ Load priorities of .env.* files (e.g. .env.production > .env) |
| 10 | +- ✅ Hierarchical cascading configuration in monorepo projects (Nx, Turbo, etc.) apps/next-app/.env > apps/.env > .env |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +```bash |
| 15 | +npx @devmy/dotenv2shell [options] |
| 16 | +``` |
| 17 | + |
| 18 | +### Options |
| 19 | + |
| 20 | +* `-o, --override`: Override existing environment variables on your machine with values from the `.env` file (default: false). |
| 21 | +* `-p, --prefix`: Prefix to filter environment variables to load (e.g., `MY_APP_`). |
| 22 | +* `-r, --root`: Root directory to search for the `.env` file (default: current working directory). |
| 23 | +* `-f, --files`: Comma-separated list of `.env` files to load (default: `.env`). |
| 24 | +* `-d, --dotenv_key`: Manually specify the `DOTENV_KEY` for decryption (if using `.env.vault`). |
| 25 | + |
| 26 | +### Examples |
| 27 | + |
| 28 | +* Load the default `.env` file from the current directory: |
| 29 | + |
| 30 | +```bash |
| 31 | +eval $(npx @devmy/dotenv2shell) |
| 32 | +``` |
| 33 | + |
| 34 | +* Load a specific `.env` file with a prefix and override existing variable: |
| 35 | + |
| 36 | +```zsh |
| 37 | +eval $(npx @devmy/dotenv2shell -f my-env.env -p MY_APP_ -o) |
| 38 | +``` |
| 39 | + |
| 40 | +## Handling Backslash Escapes |
| 41 | + |
| 42 | +In some cases, environment variables loaded from the `.env` file may contain special characters that require backslash escapes to be interpreted correctly by the shell. This is particularly relevant when dealing with paths, file names, ssh-keys or other strings that might contain characters like spaces, commas, or backslashes themselves. |
| 43 | + |
| 44 | +If you encounter issues with environment variables not behaving as expected, check for any special characters in their values and consider using backslash escapes to ensure they are treated as literal characters. For example: |
| 45 | + |
| 46 | +```bash |
| 47 | +eval $(npx @devmy/dotenv2shell) && |
| 48 | +echo -e "$DEPLOY_GITHUB_SSH_KEY" | ssh-add - |
| 49 | +``` |
| 50 | + |
| 51 | + |
| 52 | +Remember that the -e option in echo is used to enable backslash escapes when printing strings, but it's not necessary when loading environment variables. The shell will automatically handle backslash escapes when interpreting environment variable values. |
0 commit comments