在 vscode 中的 task.json 中为 WSL 设置环境变量

Yux*_*ang 17 visual-studio-code windows-subsystem-for-linux vscode-tasks

我尝试为将在 Windows 子系统 Linux 中运行的 Visual Studio Code 任务设置环境变量。然而,它似乎不起作用。这是我的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"
                }
            }

        },
    ]
}
Run Code Online (Sandbox Code Playgroud)

输出是:

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



Terminal will be reused by tasks, press any key to close it.
Run Code Online (Sandbox Code Playgroud)

请注意,默认情况下 shell 已手动修改为C:\WINDOWS\SysNative\bash.exeWSL,如此处此处建议。

Mar*_*ark 19

选项对象超出任何任务,因此:

format
  "version": "2.0.0",
  "options": {
      "env": {
        "test": "test_string"
      }
   }
   "tasks": [
    {
        "label": "test env",
        "type": "shell",
        "command": "echo",
        "args": [
            "$env:test"
        ],
    },
Run Code Online (Sandbox Code Playgroud)

然后访问选项参数,如下所示:

$env:test  or    ${env:test}
]
Run Code Online (Sandbox Code Playgroud)

  • 只有当我输入“$test”而不是“$env:test”时,它才对我有用。 (5认同)

Tho*_*Bui 6

我最终为 VS Code (v1.67.1) 所做的是:

  • 将我的环境变量添加到settings.json
  • 使用传统%ENV_VAR%选项。

所以在 中settings.json,创建这个:

{
  "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"
  }
}
Run Code Online (Sandbox Code Playgroud)

在 中tasks.json,使用正确的 ENV 变量引用:

"command": "echo %AWS_REGION%"