WSL: Files and Environment Variables

Continuing from WSL: Directories and Files.

The Ultimate Guide to Windows Subsystem for Linux (Windows WSL) points out that with WSL2 the Linux file system is a virtual disk. In my case

C:\Users\Glenn\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\ext4.vhdx

I hope there is a way to relocate this file into a directory format that is fit for human consumption.

The Ultimate Guide goes on to consider environment variables. Opening an administrator command prompt as in the example:

    Microsoft Windows [Version 10.0.19043.1083]
(c) Microsoft Corporation. All rights reserved.

C:\Windows\system32>set GLENN=c:\Users\Glenn

C:\Windows\system32>echo %GLENN%
c:\Users\Glenn

C:\Windows\system32>set WSLENV=GLENN/p

C:\Windows\system32>echo %GLENN%
c:\Users\Glenn

C:\Windows\system32>wsl
gmcdavid@Armagh:/mnt/c/Windows/system32$ echo $GLENN
/mnt/c/Users/Glenn
gmcdavid@Armagh:/mnt/c/Windows/system32$ exit
logout

C:\Windows\system32>wsl -- echo $GLENN
/mnt/c/Users/Glenn

C:\Windows\system32>

Thus replicating the results of the Ultimate Guide

Repeating the experiment. However, if I go to another WSL window, not spun off from my command prompt, echo $GLENN returns nothing.

Opening another command window, not as administrator:

Microsoft Windows [Version 10.0.19043.1083]
    (c) Microsoft Corporation. All rights reserved.
    
    C:\Users\Glenn>echo %GLENN%
    %GLENN%
    
    C:\Users\Glenn>wsl
    gmcdavid@Armagh:/mnt/c/Users/Glenn$ echo $GLENN
    
    gmcdavid@Armagh:/mnt/c/Users/Glenn$

I have lost both my Windows and Linux environmental variables. Same thing for an admin command window. Windows environment variables only last for the session they are created in.

Going to Control Panel/System and Security/System/Advanced system settings/Advanced/Environment Variables I can add GLENN there. I put set WSLENV=GLENN/p in a batch file executed at startup. I placed a shortcut to such a batch file in C:\Users\Glenn\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup (shell:startup). I verified that the file is executed by having it write to a text file when it runs:

        Microsoft Windows [Version 10.0.19043.1110]
        (c) Microsoft Corporation. All rights reserved.
        
        C:\Users\Glenn>echo %GLENN%
        c:\Users\Glenn
        
        C:\Users\Glenn>wsl
        gmcdavid@Armagh:/mnt/c/Users/Glenn$ echo $GLENN
        
        gmcdavid@Armagh:/mnt/c/Users/Glenn$ exit
        logout
        
        C:\Users\Glenn>echo %GLENN%
        c:\Users\Glenn
        
        C:\Users\Glenn>      

So GLENN exists in Windows, but not in WSL. Perhaps I need to start WSL before executing set WSLENV=GLENN/p. I am not sure I want to start WSL every time I log on. So these shared environment variables may not be as useful as I had thought. Or I have made some stupid mistake.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.