How to use an environment variable value in another environment variable name and value using dotenv?

Viewed 3875

For my testing framework I'm using dotenv to read and initialize environment variables for different test environments. Now I want to use a specific environment variable value as a part of the value and the name of another environment variables.

So, I set the env var export INSTANCE=1 before initializing the env file with dotenv.

This is my env file with all the env vars I want to initialize with dotenv:

# INSTANCE as part of env var value
MY_DOMAIN="http://mypage/"$INSTANCE""

# INSTANCE as part of env var name
SUBDOMAIN"$INSTANCE"="/mysubdomain"

And this metric is also not working:

# INSTANCE as part of env var value
MY_DOMAIN=http://mypage/${INSTANCE}

# INSTANCE as part of env var name
SUBDOMAIN${INSTANCE}=/mysubdomain

This is possible doing it via bash with

export MY_DOMAIN="http://mypage/"$INSTANCE"" SUBDOMAIN"$INSTANCE"="/mysubdomain"

echo $MY_DOMAIN
http://mypage/1

echo $SUBDOMAIN1
/mysubdomain

But using dotenv it is not working for me. I have the following code:

const dotenv = require("dotenv");
var result = dotenv.config({ path: '/path/to/my/env/file' });

console.log(result.parsed);

The console.log produces the following result:

{
  MY_DOMAIN:'http://mypage/"$INSTANCE"'
}

So, the env var INSTANCE can not be used, the value will be ignored. Are there any solution to solve my issue?

1 Answers
Related