How to change default directory in Bash for Windows 10?

WindowsBash

Windows Problem Overview


How can I change the default directory of windows bash to a folder of my choosing?

EDIT: I guess I should have been more clear. When I startup Bash I want it the directory to be in a location of my choosing like Desktop or something. How do I go about setting a default directory?

Windows Solutions


Solution 1 - Windows

If you want change the directory your bash prompt is starting in, you can edit your .bashrc file. At the bottom, add:

cd ~

This will go into your home directory. (you can actually do just cd, but I it's clearer to add the ~ IMO)


To edit, you can use vim. If you don't know how to use it, you can always use nano for the time being, but you really should have a look at it, it's really powerful.

$ nano ~/.bashrc

This will open nano in "full console". At the bottom, you have the few commands you can use (^ means control) Do your changes, hit ctrl+o to save the file (write the file). It'll ask you where to write, by default, it's the right location, just hit enter and the .bashrc file will be saved. Then, you can press ctrl+x to exit.

Solution 2 - Windows

Steps to set default directory for Bash on Ubuntu on Windows to a folder -

  1. Open Bash on Ubuntu on Windows.
  2. cd ~ to go to home directory of Ubuntu
  3. Type edit .bashrc and enter at the Bash. This will open the file in vim.
  4. Use Down Arrow or Page Down key on keyboard to go to the end of the file (there's a helpful progress bar at the bottom right corner of the Bash). At the end of this file, you will find cd ~, replace cd ~ with your desired location.
  5. Save the .bashrc file. To save the file, click esc and then type :wq and click enter.

Note:

  • To access your hard disk location make sure you include the mount directory first.
  • So if want your Bash to open at C:\dev whenever you open the Bash. You need to replace the cd ~ with cd /mnt/c/dev at .bashrc file at Ubuntu home directory.

Solution 3 - Windows

Just enter echo "cd ~" >> ~/.bashrc. This will append "cd ~" to your .bashrc.

.bashrc is executed everytime you start a(n interactive) bash instance.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondiaztho1View Question on Stackoverflow
Solution 1 - Windowsmath2001View Answer on Stackoverflow
Solution 2 - WindowsNimit BhargavaView Answer on Stackoverflow
Solution 3 - WindowsmfnalexView Answer on Stackoverflow