10

Running the command ubuntu in Windows Terminal puts the directory as linux default, but running wsl causes it to mount at /mnt/%USERPROFILE%. I know I can just run ubuntu, but I'm used to using wsl instead. I checked my Windows Terminal config and that is fine (no defined directory, using dropdown menu sets as ~). I'm sorry if this may sound confusing as I am relatively new to wsl. The terminal did not behave like this until recently. Setting StartingDirectory or CommandLine does not work. This problem may be related more to WSL than Ubuntu

Steps to Reproduce

  1. Open Windows Terminal
  2. Enter command wsl

Expected Behaviour

Open Ubuntu WSL in ~

Actual Behaviour

Opens Ubuntu WSL in /mnt/c/users/<username>

vvxrtues
  • 133

2 Answers2

6

While there's no way to change the default directory that is entered when you run wsl.exe (for good reason, see below), the best workaround is to to simply run wsl ~, which will start in the default user's home directory. For Windows Terminal, you can set the profile's "Command Line" setting to wsl ~, which will override the "Starting Directory".

More Explanation:

When any executable starts under Windows (or Linux), it inherits the "current working directory" from the application that started it. It can (and often does) set a new working directory during the startup.

The wsl.exe and ubuntu.exe commands differ a bit in their design:

  • wsl.exe is designed to install, modify, or launch WSL itself
  • ubuntu.exe is designed to install, modify, or launch the Ubuntu distribution. Like most "pre-packaged" WSL distributions, ubuntu.exe is based on the WSL distro launcher reference implementation that Microsoft provides. Here is the forked version for the Ubuntu installer/launcher.

You can examine the source code of the distro-launcher and find the WslApiLoader::WslLaunchInteractive that has a useCurrentWorkingDirectory boolean argument. This is passed to the equivalent function in the (closed-source) wslapi.dll. That API, in turns, launches Ubuntu and (through its init function):

  • Determines the default user
  • Looks up their shell
  • Starts the shell in the specified directory (~ if the above parameter is false, and the parent process working directory otherwise)
  • (and much more)

ubuntu.exe is the "easy button" version of launching Ubuntu. It doesn't provide much additional control to the user.

wsl.exe, on the other hand, has quite a few command-line arguments that allow you to control exactly what is launched and how, including:

  • The directory in which to start, either through wsl ~ (shortcut for launching in the home directory) or wsl --cd <directory>
  • The ability to launch as a different user (wsl -u <username>)
  • The ability to skip launching the default shell and run a command in its place (wsl -e <command>)
  • And more

Here's a simple example of why you should want the wsl.exe command to default to the current working directory. From PowerShell:

wsl ls -lh

This will (as mentioned in this answer) show you human readable file sizes in the directory listing, something that isn't easy to do in PowerShell otherwise.

If wsl.exe defaulted to the home directory, the equivalent command would give you a listing of files in that directory, rather than the current PowerShell directory.

Being able to use Linux commands through wsl directly in PowerShell can be quite flexible and powerful.

It's also for similar reason that I recommend against modifying ~/.bashrc to change your directory.


Alternative:

While wsl ~ is pretty short as it is (5 characters, still 1 less than ubuntu), you could also "alias" it to something like wslh (or even something shorter) in PowerShell. However, since PowerShell aliases don't support arguments, you would need to add a function to your profile.ps1. Something like:

function wslh {
    wsl ~ @args
}

This will allow you to use wslh to start in the home directory. You can still use arguments like wslh -u root -d Ubuntu-22.04 -e ls (et. al.) as well.

NotTheDr01ds
  • 28,025
-1

Personnally I removed Docker-Destop and install docker directly in Ubuntu which run under WSL2)' then I scheduled wsl at startup.

  1. my default user in ubuntu is part of docker group
  2. my docker service is enabled: systemctl enable docker
  3. and my docker compose file is in /srv/
JOduMonT
  • 154