Unable to fetch the env value from env.sh using K6 test script

Env File

The below value exists on env.sh file
export VU = 3

Test File

I am trying to fetch the value on the test file
import http from ‘k6/http’;
import { check } from ‘k6’;
console.log(${__ENV.VU})

Output:
INFO[0000] undefined

Note:

  1. Expected Result is 3
  2. env.sh file created outside the tests folder

You have a few issues here. First, the contents of env.sh are wrong, when you export values in shell scripts you shouldn’t have spaces around the = sign, so it should be like this

export VU=3

Second, exported variables get passed to child processes, not to parent ones. So, even if you had the correct env.sh and executed it like this:

./env.sh
k6 run new.js

k6 won’t be able to see the VU=3 environment variable. If you want to define your environment variables in a separate file, you’d have to use source to load it, not simply execute it, like this:

source env.sh
k6 run new.js

Alternatively, you can also directly define your environment variable in the same shell:

export VU=3
k6 run new.js

or just use the --env k6 option to pass it directly to k6:

k6 run --env VU=3 new.js
1 Like

Thanks a lot for the detailed explanation. Appreciated!