%UserProfile% 的 DrvFs 位置

DrvFs location of %UserProfile%

在 Linux 的 Windows 子系统中,我如何获得指向 Windows 个性中 %UserProfile% 的 DrvFs 路径?我看过 Access Windows Environment Variables from within Bash in WSL。获取 one 环境变量似乎相当复杂,并且无论如何都需要从 %UserProfile% 中硬编码某些内容 ($LXSS_ROOT).

这似乎有效:

win_userprofile="$(cmd.exe /c "<nul set /p=%UserProfile%" 2>/dev/null)"

win_userprofile_drive="${win_userprofile%%:*}:"
userprofile_mount="$(findmnt --noheadings --first-only --output TARGET "$win_userprofile_drive")"

win_userprofile_dir="${win_userprofile#*:}"

userprofile="${userprofile_mount}${win_userprofile_dir//\//}"

感谢 Microsoft Craig Loewen 让我入门。

幸运的是,自 Windows 10 build 17063(包含在 Windows 10 1803 中)以来,在 Windows 和 WSL (WSLENV ).

要使 %USERPROFILE% 在 WSL 中可访问,请在 WSLENV 变量中列出变量。如果您还没有使用 WSLENV 那么只需 运行 这一次 Windows:

setx WSLENV USERPROFILE/up

这将导致 Windows 中的 %USERPROFILE% 可以作为 WSL shell 中的 $USERPROFILE 访问,Windows 路径转换为 ​​Unix 格式。如果不想转换路径,省略p:

即可
setx WSLENV USERPROFILE/u

更多详情:


我在 cdw 函数中使用变量(cd 到 Windows 路径)。我在 ~/.bash_aliases 中定义它,它在 Ubuntu:

中自动执行
#!/bin/bash

cdw () {
        if test "$#" -eq 0 ; then
                cd "$USERPROFILE"
        elif test "" = - ; then
                cd ""
        else
                cd -- "$(wslpath "$@")"
        fi
}