I was thinking of adding something like this to my setup.bash script.
ln -s /mnt/c/Users/Ryan/Downloads $HOME/Downloads
But obviously that isn't always an accurate path so I was hoping to be able to do to something like
ln -s /mnt/c/Users/%USERNAME%/Downloads $HOME/Downloads
or
ln -s %USERPROFILE%/Downloads $HOME/Downloads
I know that obviously Windows % vars wouldn't work in bash but it would be cool if wsl could/does export those vars as $Win32.HOME
or $Win32.USER
or something.
Any thoughts?
Is there any way to do this already?
In newer versions of WSL2, which I believe have been backported to Win10 1093 and 1909, along with 2004, Microsoft introduced an environment variable WSLENV
that is shared between Win32 and WSL to accomplish exactly what you're after.
You set it equal to a colon-seperated list of variables, together with some syntax to trigger path conversion, and whether the variable should be shared bi-directionally, or whether it should only be shared from Win32-->WSL2 or WSL2-->Win32.
For your specific use-case, on Win32 set WSLENV=USERPROFILE/p
. That will trigger the Win32 env variable %USERPROFILE%
to be passed to WSL (and back to Win32 if you call cmd
or similar once you're in WSL), and to perform path translation.
Now in your setup.bash
script, you can do
ls -s $USERPROFILE/Downloads $HOME/Downloads
For the various syntax of this functionality, see the Microsoft article Share Environment Vars between WSL and Windows.
Remembering that it's a shell's job to evaluate environment variables, and since you can invoke Windows exe's from within Linux on WSL, you can ask a Windows shell (Cmd or PowerShell) to expand out a Windows env-var. Here's how you'd do that with Cmd:
$ cmd.exe /c echo %username%
richturn
$
Alternatively, you can choose to project some of your Windows environment variables into WSL if you prefer :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With