20

I tried to set environment variables for my Visual Studio Code tasks that will run in my Windows Subsystem Linux. However, it does not seem to work. Here is my tasks.json:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "test env",
            "type": "shell",
            "command": "echo",
            "args": [
                "$test"
            ],
            "options": {
                "env": {
                    "test": "test_string"
                }
            }

        },
    ]
}

The output is:

> Executing task in folder ex12-test: echo $test <



Terminal will be reused by tasks, press any key to close it.

Note that by default shell has been manually modified to C:\WINDOWS\SysNative\bash.exefor WSL, as recommended here and here.

3
  • 1
    See environment variable not found In your case you need to use $env:test Commented Jan 17, 2019 at 3:57
  • @Mark Thank you! I changed to ${env:test} and it still wouldn't work. It seems like the environment variable wasn't set at all. Commented Jan 18, 2019 at 6:29
  • 1
    @YuxiangWang the answer is here. You simply cannot reference environment variables set in this manner in the args or command of your tasks.json task definition. Commented Jul 4, 2019 at 11:58

3 Answers 3

23

The options object goes outside of any task, so:

format
  "version": "2.0.0",
  "options": {
      "env": {
        "test": "test_string"
      }
   }
   "tasks": [
    {
        "label": "test env",
        "type": "shell",
        "command": "echo",
        "args": [
            "$env:test"
        ],
    },

And then access the options argsuments like so:

$env:test  or    ${env:test}
]
Sign up to request clarification or add additional context in comments.

4 Comments

It only worked me if I put "$test" instead of "$env:test".
So it is not possible to set an environment variable for a specific task only?
@godo it's possible here: code.visualstudio.com/Docs/editor/tasks#_custom-tasks. Seems like you'd need to add an "options" object to the specific task
also for me it only worked with "${test}" instead of "${env:test}"
6

What I eventually did for my VS Code (v1.67.1) is:

  • Add my env variables into a settings.json
  • Use the traditional %ENV_VAR% option.

So in the settings.json, create this:

{
  "terminal.integrated.env.windows": { // switch to your os, check `Open Workspace Settings` in the Command Pallete then search for `environment`
    "AWS_REGION": "us-east-2", 
    "AWS_ID": "my_id"
  }
}

In the tasks.json, use the proper ENV variable reference:

"command": "echo %AWS_REGION%"

1 Comment

In tasks.json, worked me only this: "command": "echo ${env:AWS_REGION}" It depends on used shell.
0

If you need to set some environment variables for all tasks of the project, then you could skip configuring tasks.json and use direnv. Using this tool to set project-specific environment variables is mentioned in Gradle for Java - Visual Studio Code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.