In Windows Terminal running Ubuntu, how can I make the default directory be ~ (/home/jake) instead of / (root)?
772
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?
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 /.
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.
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
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.
If you're uncomfortable in changing command-line, you can still get it to work by modifying startDirectory to
Seeing some answers in here, and knowing this is an old thread, I still wanted to respond to those stating to put "cd ~" in the .bashrc file. While that works, that means every interactive non-login shell reads .bashrc first. That means whether it's the first terminal opened, or every subsequent terminal/bash process is spawned from the initial "login" terminal, you will always end up in the home directory.
You can test this by editing .bashrc putting the "cd ~" command in it. Exit Windows terminal. Then launch Ubuntu again. You'll be in the home directory. Now, navigate to a different directory within that terminal session and then execute "bash". You'll end up in your home directory which might not be what you wanted.
This same issue happens if you are using Visual Studio code. In VSC, you have the ability to create terminal sessions. Many are expecting that session to open up in whatever directory they launched VSC from (mostly a project they are working on). However, that will execute the commands in .bashrc which means you'll end up in your home directory.
.bashrc is suited for commands that are specific to the Bash shells. So, putting aliases and other Bash related functions into .bashrc is good.
A better place to put the "cd ~" is in the .profile file. This way, upon the initial terminal bash launch, it will execute the "cd ~" command in .profile. If you then launch VSC from a different directory, when you create a terminal session within VSC, it will stay in the directory you launched VSC from which is what most programmers would expect.
After doing this and removing "cd ~" from .bashrc, launch a new terminal session. You'll be in your home directory. Then, navigate to a different directory and type "bash". You'll start a new bash session but stay in the directory you were last in.
If you launch a new Ubuntu terminal inside Windows Terminal, you'll be back in your home directory since it's a brand new Terminal login session.
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:
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 runwsl.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.
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.
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 toNote: If you are using Ubuntu 18.04 or 16.04 app, the command would vary accordingly. The above is only for Ubuntu App.
If you're uncomfortable in changing command-line, you can still get it to work by modifying
startDirectory
toCredits: caksoylar 's comment
You can do the following:
Open WSL and edit your
~/.bashrc
file usingnano
:At the bottom of the file add the following line:
Save and close the file by pressing Ctrl+O followed by Ctrl+X.
Finally, restart WSL.
Seeing some answers in here, and knowing this is an old thread, I still wanted to respond to those stating to put "cd ~" in the
.bashrc
file. While that works, that means every interactive non-login shell reads.bashrc
first. That means whether it's the first terminal opened, or every subsequent terminal/bash process is spawned from the initial "login" terminal, you will always end up in the home directory.You can test this by editing
.bashrc
putting the "cd ~" command in it. Exit Windows terminal. Then launch Ubuntu again. You'll be in the home directory. Now, navigate to a different directory within that terminal session and then execute "bash". You'll end up in your home directory which might not be what you wanted.This same issue happens if you are using Visual Studio code. In VSC, you have the ability to create terminal sessions. Many are expecting that session to open up in whatever directory they launched VSC from (mostly a project they are working on). However, that will execute the commands in
.bashrc
which means you'll end up in your home directory..bashrc
is suited for commands that are specific to the Bash shells. So, putting aliases and other Bash related functions into.bashrc
is good.A better place to put the "cd ~" is in the
.profile
file. This way, upon the initial terminal bash launch, it will execute the "cd ~" command in.profile
. If you then launch VSC from a different directory, when you create a terminal session within VSC, it will stay in the directory you launched VSC from which is what most programmers would expect.After doing this and removing "cd ~" from
.bashrc
, launch a new terminal session. You'll be in your home directory. Then, navigate to a different directory and type "bash". You'll start a new bash session but stay in the directory you were last in.If you launch a new Ubuntu terminal inside Windows Terminal, you'll be back in your home directory since it's a brand new Terminal login session.
Hope that helps!