Set environment variables in task.json in vscode for WSL

10,377

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}
]
Share:
10,377
Yuxiang Wang
Author by

Yuxiang Wang

I am a researcher in haptics / biomechanics / computational neuroscience.

Updated on June 04, 2022

Comments

  • Yuxiang Wang
    Yuxiang Wang almost 2 years

    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.

  • juanmah
    juanmah almost 4 years
    It only worked me if I put "$test" instead of "$env:test".
  • godo
    godo about 2 years
    So it is not possible to set an environment variable for a specific task only?
  • Thomas Bui
    Thomas Bui almost 2 years
    @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