如何在 Windows 10 的 Bash 中更改默认目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42324355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:48:45  来源:igfitidea点击:

How to change default directory in Bash for Windows 10?

windowsbash

提问by diaztho1

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

如何将 windows bash 的默认目录更改为我选择的文件夹?

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?

编辑:我想我应该更清楚。当我启动 Bash 时,我希望它的目录位于我选择的位置,比如桌面或其他东西。如何设置默认目录?

回答by math2001

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

如果您想更改 bash 提示符所在的目录,您可以编辑您的.bashrc文件。在底部添加:

cd ~

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

这将进入您的主目录。(你实际上可以只做cd,但我更清楚地添加~IMO)



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

要编辑,您可以使用vim. 如果你不知道怎么用,nano暂时可以一直用,但你真的应该看看它,它真的很强大。

$ 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+oto save the file (write the file). It'll ask you where to write, by default, it's the right location, just hit enterand the .bashrcfile will be saved. Then, you can press ctrl+xto exit.

这将在“完整控制台”中打开 nano。在底部,您有几个可以使用的命令(^意味着control)进行更改,点击ctrl+o以保存文件(写入文件)。它会问你在哪里写的,默认情况下,这是正确的位置,正好碰到enter.bashrc文件将被保存。然后,您可以按ctrl+x退出。

回答by Nimit Bhargava

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

在 Windows 上的 Ubuntu 上将 Bash 的默认目录设置为文件夹的步骤 -

  1. Open Bash on Ubuntu on Windows.
  2. cd ~to go to home directory of Ubuntu
  3. Type edit .bashrcand enter at the Bash. This will open the file in vim.
  4. Use Down Arrowor Page Downkey on keyboard to go to the end of the file (there's a helpful progress barat 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 escand then type :wqand click enter.
  1. 在 Windows 上的 Ubuntu 上打开 Bash。
  2. cd ~转到Ubuntu的主目录
  3. edit .bashrc在 Bash 中键入并输入。这将在 vim 中打开文件。
  4. 使用Down ArrowPage Down键盘上的键转到文件的末尾(Bash 的右下角有一个有用的进度条)。在此文件的末尾,您会找到cd ~,替换cd ~为您想要的位置。
  5. 保存 .bashrc 文件。要保存文件,请单击esc,然后键入:wq并单击 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:\devwhenever you open the Bash. You need to replace the cd ~with cd /mnt/c/devat .bashrc file at Ubuntu home directory.
  • 要访问您的硬盘位置,请确保首先包含安装目录。
  • 因此,如果希望您的 Bash 在C:\dev您打开Bash 时随时打开。您需要更换cd ~cd /mnt/c/dev在Ubuntu的在主目录的.bashrc文件。

回答by mfnalex

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

只需输入echo "cd ~" >> ~/.bashrc。这会将“cd ~”附加到您的 .bashrc。

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

.bashrc 每次启动(n 交互式)bash 实例时都会执行。