Using `wslinfo --env` to read Windows environment variables
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 33.7k
- Forks
- 1.8k
- Avg merge
- 3d 17h
- Merged PRs (30d)
- 116
Description
**PROBLEM**
Currently, one of the most practical methods for accessing Windows environment variables in WSL is by listing the required variables in `WSLENV` on the Windows host. However, this approach presents a challenge, as shell scripts running on WSL cannot modify this configuration on the host. An alternative is to read the output of commands like `cmd.exe /C set VARNAME` in Linux, but this method is inelegant and unreliable. For example, when using `sudo` to execute a script, `cmd.exe` becomes inaccessible, which requires either instructing the user to pass additional environment-related arguments to `sudo` to access the Windows `PATH`, or specifying the full path to `cmd.exe` in the script. Both of these solutions are neither elegant nor reliable.
**SOLUTION**
I believe that using `wslinfo --env` would be the most efficient and reliable method for accessing Windows environment variables within shell scripts. For example:
```
$ wslinfo --env=LOCALAPPDATA
C:\Users\john\AppData\Local
$ wslinfo --env=HOMEDRIVE
C:
```
Moreover, this command may also support the additional `-p` and `-l` flags, similar to their usage with `WSLENV`. Specifically, the `-p` flag converts a Windows path into a Linux path, while the `-l` flag converts a Windows semicolon-delimited path list into a Linux colon-delimited path list. For example:
```
$ wslinfo --env=LOCALAPPDATA
C:\Users\john\AppData\Local
$ wslinfo --env=LOCALAPPDATA -p
/mnt/c/Users/john/AppData/Local
$ wslinfo --env=PATHLIST
C:\Users\john;C:\Users\mike;C:\Users\rose
$ wslinfo --env=PATHLIST -l
/mnt/c/Users/john:/mnt/c/Users/mike:/mnt/c/Users/rose
```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the `wslinfo` command entry point and the existing handling of `WSLENV`. Check how environment values and path conversions are exposed, then compare the behavior with the examples in this issue. Done means `wslinfo --env` reads the requested Windows variable and the proposed `-p` and `-l` conversions behave as documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- cli, operating-systems
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100