In Windows Terminal running Ubuntu, how can I make the default directory be ~ (/home/jake) instead of / (root)?

21,395

Solution 1

This is an issue raised on Microsoft Terminal's GitHub Page in Windows Terminal as startingDirectory setting issue for wsl profile #592 . And not respecting things is a hobby of some things.

Actually it seems that startingDirectory Windows Terminal doesn't work well under WSL fs paths since the issue isn't specific to $HOME, it exists for all folders in /.

While the user3140225's approach is a good start but as per DHowett-MSFT's comment:

This is also a great way to get weird and unexpected behaviour in everything else that uses bash on your machine. 😄

Since in Ubuntu's profile you've provided the command line wsl.exe -d Ubuntu. This command line invokes WSL and ask it to start "Ubuntu" which is done in the current directory, i.e. if you run wsl.exe -d Ubuntu while in Desktop of Windows, Ubuntu will start on Windows' Desktop.

As a workaround, you can modify the command line and use one of the below.

  1. Since WSL is specifically for Linux, thus Tilde Expansion can be expected from that. Therefore, you can specify the "starting directory" in the command line and use the following as the value of command line.

    wsl.exe ~ -d ubuntu
    
  2. When Ubuntu app is installed, it automatically registers ubuntu command. Therefore, ubuntu command will invoke Ubuntu App. The benefit of this is that it always starts at $HOME. Therefore, you can change the value of command-line to

    ubuntu
    

    Note: If you are using Ubuntu 18.04 or 16.04 app, the command would vary accordingly. The above is only for Ubuntu App.

  3. If you're uncomfortable in changing command-line, you can still get it to work by modifying startDirectory to

    "startingDirectory":"//wsl$/Ubuntu/home/jake/"
    

    Credits: caksoylar 's comment

Solution 2

You can do the following:

  1. Open WSL and edit your ~/.bashrc file using nano:

    nano ~/.bashrc
    
  2. At the bottom of the file add the following line:

    cd ~
    
  3. Save and close the file by pressing Ctrl+O followed by Ctrl+X.

  4. Finally, restart WSL.

Share:
21,395

Related videos on Youtube

Jake Nixon
Author by

Jake Nixon

Updated on September 18, 2022

Comments

  • Jake Nixon
    Jake Nixon over 1 year

    I have both Windows Terminal and Ubuntu installed from the Microsoft Store inside of Windows 10 (I don't know if this is WSL Version 1 or Version 2. The wsl -l -v command failed.).

    My default directory is set to the right directory but it always opens up to root. How can I fix this config?

    enter image description here

    • Jake Nixon
      Jake Nixon over 4 years
      In Powershell wsl -l works, but the -v breaks the command and output from wsl -h is returned. The commands from the link you posted did not work either. I edited my post yesterday but this question appears to be dead. What can I do to improve it?
    • K7AAY
      K7AAY over 4 years
    • Kulfy
      Kulfy over 4 years
      @K7AAY -v doesn't work in WSL1
    • K7AAY
      K7AAY over 4 years
      Aha! A clew has been found!
  • Jake Nixon
    Jake Nixon over 4 years
    Thanks for all of this information, including the page about Tilde Expansion. I changed the JSON configuration inside of Windows Terminal to add the ~ to wsl.exe -d ubuntu and my problem is solved. I deleted the cd ~ from my .bashrc file to avoid any strange behavior from other bash utilities. Thanks very much!
  • dessert
    dessert over 4 years
    In one line: echo 'cd ~' >>~/.bashrc
  • SentientFlesh
    SentientFlesh over 3 years
    I would like to verify that @user3140225 provided the most streamlined answer to this question. No permutation of changing the "startingDirectory" value in the Windows Terminal settings.json correctly navigated me to the correct home directory you would get when launching WSL in its own shell. It seems that the configuration file does not accept Linux syntax as arguments. Adding cd ~ to your .bashrc file from within Ubuntu was the easiest and best option at this point, with @dessert providing the quickest means to do so in one line from $HOME directory: echo 'cd ~' >>~/.bashrc
  • NotTheDr01ds
    NotTheDr01ds almost 3 years
    Agreed, .profile is probably better than .bashrc, but both can create issues for the unsuspecting user (or script). Better yet to just use wsl ~ as in the accepted answer.