我遇到的问题是每次打开Bash时,都会进入一个我不想要的目录,因此我必须输入命令cd /mnt/c 才能访问我的PC文件,例如下载、文档等。如何在打开Bash时设置默认目录?
更新: 预发布版本构建17666 现在已经内置了上下文菜单 - 但是,需要您在右键单击之前按住Shift才能访问它;因此,如果您希望该命令显示为无条件的,则以下解决方案可能仍然有用。
如果将以下文本保存在*.reg
文件中并打开(双击)它,则会提示您将定义导入到用户特定的注册表中。
导入后,您将在文件资源管理器中文件夹的上下文菜单中找到一个名为Bash Console Here
的命令,并且当您单击文件夹内的空白区域时也会出现。
选择该命令后,将在该文件夹中打开一个Bash控制台窗口。
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\BashConsoleHere]
@="Bash Console Here"
"Icon"=hex(2):25,00,4c,00,4f,00,43,00,41,00,4c,00,41,00,50,00,50,00,44,00,41,\
00,54,00,41,00,25,00,5c,00,6c,00,78,00,73,00,73,00,5c,00,62,00,61,00,73,00,\
68,00,2e,00,69,00,63,00,6f,00,00,00
[HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\BashConsoleHere\command]
@="cmd /c cd \"%V\" && bash.exe"
[HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\BashConsoleHere]
@="Bash Console Here"
"Icon"=hex(2):25,00,4c,00,4f,00,43,00,41,00,4c,00,41,00,50,00,50,00,44,00,41,\
00,54,00,41,00,25,00,5c,00,6c,00,78,00,73,00,73,00,5c,00,62,00,61,00,73,00,\
68,00,2e,00,69,00,63,00,6f,00,00,00
[HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\BashConsoleHere\command]
@="cmd /c cd \"%1\" && bash.exe"
或者,您可以使用以下 PowerShell 代码片段 创建注册表项:
$null = 0..1 | % {
$key = New-Item -Force ('HKCU:\Software\Classes\Directory{0}\shell\BashConsoleHere' -f ('', '\Background')[$_])
$key | New-ItemProperty -Name '(Default)' -Value 'Bash Console Here' -Type String
$key | New-ItemProperty -Name 'Icon' -Value '%LOCALAPPDATA%\lxss\bash.ico' -Type ExpandString
$key = New-Item -Force "$($key.PsPath)\command"
$key | New-ItemProperty -Name '(Default)' -Value "cmd /c cd `"$(('%1', '%V')[$_])`" && bash.exe" -Type String
}
要稍后删除条目:
0..1 | % {
Remove-Item -Recurse -LiteralPath ('HKCU:\Software\Classes\Directory{0}\shell\BashConsoleHere' -f ('', '\Background')[$_])
}
.bashrc
文件中,并将其放在您的$HOME
目录中(通常为/home/user/
):
cd /mnt/c
请注意,正如Jim所说,“这可能会破坏任何类型的Open bash here
快捷方式”。$HOME
目录设置为/mnt/c
。/mnt/c
始终已挂载。/mnt/c
具有完全的读/写/执行权限。/mnt/c
对于您不想让其访问的任何人都是不可访问的——您的$HOME
目录将包含.ssh/
,这个文件夹必须是私有的。$HOME
目录中的所有文件复制/移动到/mnt/c
中。usermod --home /mnt/c
或者通过更改/etc/passwd
中的主目录。
注意:操作不当可能导致无法登录或使您的帐户易受攻击。
.profile
或.bashrc
文件,并将cd命令作为其最后一部分。这可能会破坏任何类型的“在此处打开bash”快捷方式。 - Jim Deville