Environment variables in Linux are supported now in VSCode (although I don't know since when or which version exactly). I have VSCode for Linux 1.73.1.

You can now use the following (just like in the question above):

{
    "java.home": "${env:HOME}/.sdkman/candidates/java/8.0.222.hs-adpt/"
}
Answer from Hechi on Stack Overflow
🌐
Visual Studio Code
code.visualstudio.com › docs › reference › variables-reference
Variables reference
November 3, 2021 - The predefined variables are supported in a select number of setting keys in settings.json files such as the terminal cwd, env, shell and shellArgs values.
🌐
Steve Kinney
stevekinney.com › courses › visual studio code › controlling settings with environment variables in visual studio code
Controlling Settings with Environment Variables in Visual Studio Code | Visual Studio Code | Steve Kinney
You can reference environment variables in settings.json using the ${env:VARIABLE_NAME} syntax. { "terminal.integrated.defaultProfile.windows": "${env:WSL_DISTRO_NAME}", // Use WSL distro name as default terminal profile on Windows ...
🌐
Orchestra
getorchestra.io › guides › using-os-environ-with-local-settings-in-vs-code-settings-json
Using os.environ with Local Settings in VS Code settings.json | Orchestra
February 11, 2025 - By leveraging os.environ, you can reference environment variables from within your code or settings files, keeping your sensitive information out of reach from version control. VS Code allows developers to customize their workspace configuration through settings.json.
Top answer
1 of 3
4

Here is an example, you set the value in the environment block.

    {
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "name": "g++ - (GDB 9.2) Build and debug active file with RepoCodeInspection",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/bin/${fileBasenameNoExtension}",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [
            {
                "description": "same as commands below by using 'setenv ...",
                "info": "cant debug b/c of libBase/libRecipe now requiring dependency to boost for stacktrace dumps",
                "name": "LD_LIBRARY_PATH",
                "value": "/libs/:./"
            }
        ],
        "externalConsole": false,
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ],
        "logging": {
            "trace": false,
            "traceResponse": false
        },
        "preLaunchTask": "RepoCodeInspection",
        
    },
    {
        "name": "g++ - (GDB 9.2) Attach to a running process",
        "type": "cppdbg",
        "request": "attach",
        "processId":"${command:pickProcess}",
        "program": "${fileDirname}/bin/${fileBasenameNoExtension}",
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ],
        "logging": {
            "trace": false,
            "traceResponse": false
        },
        "preLaunchTask": "RepoCodeInspection",
       
    },
]

}

2 of 3
3

To set multiple environment variables, use comma-separated "name", "value" pairs.

Example:

"environment": [{"name": "LD_LIBRARY_PATH", "value": "/ld/library/path/"},
                {"name": "CUDA_VISIBLE_DEVICES", "value": "0"}
               ]
🌐
Reddit
reddit.com › r/vscode › using environmental variable in settings.json
r/vscode on Reddit: Using environmental variable in settings.json
September 15, 2024 -

Could anyone tell me why this doesn't work?

{
    "roc-lang.language-server.exe": "${env:ROC_LANGUAGE_SERVER_PATH}",
    // If you'd like to format Roc files on save
    "editor.formatOnSave": true
}

This is in the workspace settings.json for a project. When I look at the plugin's output, it's treating that exact string as the path, rather than resolving it to the value of the environmental variable. A few other things to note:

  1. If I instead use the literal string that is the value of the variable, it works fine.

  2. I am able to echo that variable in vs code's terminal, so I know it's available.

  3. I am on on linux with a reasonably recent version of vs code.

Thanks.

EDIT: Maybe this is just a limitation of the roc language plugin, that it isn't programmed to resolve the variable?

🌐
Reddit
reddit.com › r/vscode › my settings.json file can't read variables in my .env file.
r/vscode on Reddit: my settings.json file can't read variables in my .env file.
January 27, 2023 -

I am trying to override the default python interpreter for a specific workspace. I have created a .vscode directory in the folder with the following two files:

settings.json:

{
    "python.envFile": "${workspaceFolder}/.vscode/.env",
    "python.defaultInterpreterPath": "${env:MY_PYTHON_PATH}/bin/python",
}

.vscode/.env:

MY_PYTHON_PATH=/path/to/my/python/interpreter

But the python interpreter is actually getting set to /bin/python - which implies that ${env:MY_PYTHON_PATH} is just empty.

Any ideas what is going wrong here?

Find elsewhere
🌐
GitHub
github.com › microsoft › vscode › issues › 205327
Variables Support in Setting.json · Issue #205327 · microsoft/vscode
January 14, 2024 - VS Code settings in Setting.json doesn't support variables(such as ${userHome},${env:USERNAME}). It spoils the experience. Some plugins(such as Run Terminal Command) support environment variables in settings.json.
Author   xiaoxstz
🌐
GitHub
github.com › microsoft › vscode › issues › 114515
`${env:VARIABLE}` not work sometimes in `settings.json` · Issue #114515 · microsoft/vscode
October 28, 2020 - So why VS Code did not recognize the JAVA_HOME variable? ... In settings.json, set java.configuration.maven.userSettings to ${env:M2_HOME}\\conf\\settings.xml, and M2_HOME="D:\Program Files\maven".
Author   woobhurk
Top answer
1 of 15
123

Assuming you mean for a debugging session(?) then you can include a env property in your launch configuration.

If you open the .vscode/launch.json file in your workspace or select Debug > Open Configurations then you should see a set of launch configurations for debugging your code. You can then add to it an env property with a dictionary of string:string.

Here is an example for an ASP.NET Core app from their standard web template setting the ASPNETCORE_ENVIRONMENT to Development :

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": ".NET Core Launch (web)",
      "type": "coreclr",
      "request": "launch",
      "preLaunchTask": "build",
      // If you have changed target frameworks, make sure to update the program path.
      "program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/vscode-env.dll",
      "args": [],
      "cwd": "${workspaceFolder}",
      "stopAtEntry": false,
      "internalConsoleOptions": "openOnSessionStart",
      "launchBrowser": {
        "enabled": true,
        "args": "${auto-detect-url}",
        "windows": {
          "command": "cmd.exe",
          "args": "/C start ${auto-detect-url}"
        },
        "osx": {
          "command": "open"
        },
        "linux": {
          "command": "xdg-open"
        }
      },
      "env": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "sourceFileMap": {
        "/Views": "${workspaceFolder}/Views"
      }
    },
    {
      "name": ".NET Core Attach",
      "type": "coreclr",
      "request": "attach",
      "processId": "${command:pickProcess}"
    }
  ]
}

2 of 15
73

You can load an environment file by setting the envFile property like this:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch",
      "type": "go",
      "request": "launch", 
      "mode": "debug",
      "remotePath": "",
      "port": 2345,
      "host": "127.0.0.1",
      "program": "${workspaceFolder}",
      "envFile": "${workspaceFolder}/.env", // HERE
      "args": [], 
      "showLog": true
    }
  ]
}

Place the .env file in your folder and add vars like this:

KEY1="TEXT_VAL1"
KEY2='{"key1":val1","key2":"val2"}'

Further Reading: Debugging go in vscode with environment variables

🌐
Visual Studio Code
code.visualstudio.com › remote › advancedcontainers › environment-variables
Environment variables
November 3, 2021 - Dockerfile or image: Add the containerEnv property to devcontainer.json to set variables that should apply to the entire container or remoteEnv to set variables for VS Code and related sub-processes (terminals, tasks, debugging, etc.):
🌐
Visual Studio Code
code.visualstudio.com › docs › python › environments
Python environments in VS Code
November 3, 2021 - When you assign an environment to a project, the extension writes to your workspace settings (.vscode/settings.json):
🌐
GitHub
github.com › Microsoft › vscode › issues › 43351
Possible to support variables in settings.json? · Issue #43351 · microsoft/vscode
Maven stores downloaded JARs to ~/.m2. On Linux ~ expands to /home/username, while on MacOS it expands to /Users/user.name. Now if I could use an environment variable in the settings.json file, I could keep the setting platform agnostic in the form of ${env:HOME}/.m2/path/to/lombok.jar
Author   ghost
🌐
GitHub
github.com › microsoft › vscode › issues › 8147
Support using environment variables in settings.json · Issue #8147 · microsoft/vscode
March 6, 2016 - It would be nice to be able to use environment variables in paths in settings.json, this is not possible in build 1.2.1 and older. In my scenariio I`m syncing VS Code settings between computers, for example: // Specifies the path to a PowerShell Script Analyzer settings file. Use either an absolute path (to override the default settings for all projects) or use a path relative to your workspace. "powershell.scriptAnalysis.settingsPath": "%home%.vscode\PSScriptAnalyzerSettings.psd1"
Author   janegilring
🌐
GitHub
github.com › golang › vscode-go › issues › 2395
Environment Variable interpolation in settings.json · Issue #2395 · golang/vscode-go
February 9, 2022 - # .vscode/settings.json { "go.testEnvVars": { "FOOBAR": "foo ${env:BAR}" }, } Describe the solution you'd like Strings of the form ${env:FOOBAR} should be replaced with the environment variable value. Describe alternatives you've considered You'd think this should be implemented in VSCode but that's not going to happen.
Author   hrobertson
🌐
Visual Studio Code
code.visualstudio.com › docs › getstarted › settings
User and workspace settings
November 3, 2021 - You can access the workspace settings ... Linux Ctrl+,)) Select the Preferences: Open Workspace Settings (JSON) command in the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P))...
Top answer
1 of 5
13

Currently you cannot set variables in the settings.json file.

The current open issue for VS Code to implement this feature is here: https://github.com/microsoft/vscode/issues/2809

It has yet to have a PR and the opening of the issue occurred Feb 2016, but with comments within the last 2 months.

2 of 5
3

Quoting from the same docs which were linked in the question post (asker just needed to scroll down) https://code.visualstudio.com/docs/editor/variables-reference#_is-variable-substitution-supported-in-user-and-workspace-settings :

Is variable substitution supported in User and Workspace settings?

The predefined variables are supported in a select number of setting keys in settings.json files such as the terminal cwd, env, shell and shellArgs values. Some settings like window.title have their own variables:

"window.title": "${dirty}${activeEditorShort}${separator}${rootName}${separator}${appName}"

Refer to the comments in the Settings editor (Ctrl+,) to learn about setting specific variables.

Even quoting from the very first sentence in that entire document:

Visual Studio Code supports variable substitution in Debugging and Task configuration files as well as some select settings. Variable substitution is supported inside some key and value strings in launch.json and tasks.json files using ${variableName} syntax.

If you want to be able to use variables freely in settings.json, then see Support variables when resolving values in settings #2809, give it a thumbs up to show support for it, and subscribe to it to get notified about discussion and progress. Please avoid making comments there like "+1" / "bump" / "still not implemented??!!1?!?1".

🌐
Medium
reuvenharrison.medium.com › using-visual-studio-code-to-debug-a-go-program-with-environment-variables-523fea268271
Debugging go in vscode with environment variables | by Reuven Harrison | Medium
August 30, 2020 - Debugging go in vscode with environment variables When running tests in vscode you often need environment variables, some of which may have json values. Option 1: settings.json Open settings.json and …