3

I have WSL (Windows Subsystem for Linux) set up on my computer with Ubuntu.  When I want a Linux session, I hit Win+R and type bash because it's short and quick.  However, it always drops me into /mnt/c/Users/Michael.  I want it to drop me into my Ubuntu home directory (~) instead.

I've tried adding cd ~ to my .bash_profile, but then whenever I try to run bash from the command line, it always takes me to ~, instead of opening bash in the current directory.

I can get around this by running ubuntu from the Run Dialog, but it takes longer and is harder to spell.

How can I get bash to open ~ when run from the run dialog, and the current working directory when run from Command Prompt or PowerShell? Or, what are some good workarounds?

4
  • Probably all you need is to create a command (bat, shortcut, whatever) for windows that will force a shell to open where you want (your home). Than to call it from Dialog. From this answer you can execute the equivalent of a sh -c 'cd ~/Documents; exec "${SHELL:-sh}"' (you may want to substitute sh with bash and delete Documents too since you want only ~ )... But I hope you get the hint. – Hastur Mar 13 '20 at 10:47
  • @Hastur The problem with that is that it will always open in that same directory then, even when I run bash from the command line. – Michael Kolber Mar 16 '20 at 2:02
  • I believe that you’ve misunderstood Hastur’s suggestion.  I believe that their suggestion is that you create a batch file with a new name (e.g., wsl.bat or runbash.bat) containing the sh -c '…' command, and then run that from the Run dialog, while continuing to use bash from Command Prompt or PowerShell.  Or create a Windows shortcut, and put that in your Start menu or on your desktop. – Scott Mar 16 '20 at 2:15
  • Oh, I understand now – Michael Kolber Mar 16 '20 at 2:47
3

A simple kludge (workaround) would be to put

if [ "$PWD" = '/mnt/c/Users/Michael' ]
then
    cd
fi

into your .bash_profile.  That way, if you’re in your Windows home directory (C:\Users\Michael) when you run bash, it will go to your Ubuntu home directory.  If you’re anywhere else, it will stay there.

This is a kludge/workaround inasmuch as, if you manually go to C:\Users\Michael in Command Prompt or PowerShell, bash will still cd to ~.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.