如何在 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
How to change default directory in Bash for Windows 10?
提问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 .bashrc
file. 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 nano
for 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+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.
这将在“完整控制台”中打开 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 的默认目录设置为文件夹的步骤 -
- Open Bash on Ubuntu on Windows.
cd ~
to go to home directory of Ubuntu- Type
edit .bashrc
and enter at the Bash. This will open the file in vim. - Use
Down Arrow
orPage Down
key 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 findcd ~
, replacecd ~
with your desired location. - Save the .bashrc file. To save the file, click
esc
and then type:wq
and click enter.
- 在 Windows 上的 Ubuntu 上打开 Bash。
cd ~
转到Ubuntu的主目录edit .bashrc
在 Bash 中键入并输入。这将在 vim 中打开文件。- 使用
Down Arrow
或Page Down
键盘上的键转到文件的末尾(Bash 的右下角有一个有用的进度条)。在此文件的末尾,您会找到cd ~
,替换cd ~
为您想要的位置。 - 保存 .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:\dev
whenever you open the Bash. You need to replace thecd ~
withcd /mnt/c/dev
at .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 实例时都会执行。