Advanced

dotenvx run - Shell Expansion

Prevent your shell from expanding inline `$VARIABLES` before dotenvx has a chance to inject them. Use a subshell.

$ dotenvx run --env="HELLO=World" -- sh -c 'echo Hello $HELLO'
Hello World

Background

Given your .env file looks like this,

.env

HELLO=World

You might assume running dotenvx run -- echo "Hello $HELLO" would print Hello World. But, that's not what happens.

Why? The shell processes the echo "Hello $HELLO" command first before passing it to dotenvx. That's how shells work. They don't read from left to right like a human. Instead, they first convert any variables like $HELLO. As a result, $HELLO gets converted to empty string '' before dotenvx run has a chance to run. The result is Hello .

There are two solutions to this:

  1. subshell
  2. subscript

Subshell

As detailed above, use a subshell.

$ dotenvx run -- bash -c 'echo Hello $HELLO'

Make sure to use single quotes ' so values are NOT interpreted.

Subscript

Or you can encapsulate in a script. Here's an example using npm scripts.

package.json

{
  "scripts": {
    "_echo": "echo Hello $HELLO",
    "hello": "dotenvx run -- npm run _echo"
  }
}
$ npm run hello
Hello World