Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a directory by default on BASH windows [closed]

Tags:

bash

unix

windows

The problem I am having is that every time I open bash, it takes me to an unwanted directory, so I have to type the commands cd /mnt/c to get to my PC files like downloads, documents etc. How do I set a directory by default when I open bash?

like image 443
daniel Avatar asked Feb 26 '17 23:02

daniel


1 Answers

The bash.exe Windows executable that starts Bash on Windows implicitly makes the current directory its startup directory.

That means that you can simply modify the shortcut file that opens Bash and change its Start in: field to open Bash in the directory of choice (specify a regular Windows path; it is automatically translated into a /mnt/c-prefixed path when Bash starts).

  • Caveat: The official Bash on Ubuntu on Windows shortcut file (in the Start Menu) passes ~ as the startup directory as part of the Target: field; simply remove  ~ from the C:\Windows\System32\bash.exe ~ value in Target:, and then fill in the Start in: field.

Of course, you can create new shortcut files, each with its own startup directory, if desired.


Adding a Bash Console Here command to the context menu of folders in File Explorer

Update: Prerelease build 17666 now comes with a context menu built in - however, it requires you to hold down Shift before right-clicking in order to access it; the solution below may therefore still be of interest if you want the command to show unconditionally.

If you save the following text in a *.reg file and open (double-click) it, you'll be prompted to import the definitions into your user-specific registry hive.

After import, you'll find a Bash Console Here command in the context menu of folders in File Explorer, and also when you click in the empty space inside a folder.
On selecting that command, a Bash console window will open in that folder.

Caveat: Because an aux. cmd.exe call must be used to change the directory before invoking bash.exe, the console window will have cmd.exe's icon and will be grouped with regular cmd.exe in the taskbar.

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"

Alternatively, you can use the following PowerShell snippet to create the registry entries:

$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
}

To remove the entries later:

0..1 | % {
  Remove-Item -Recurse -LiteralPath ('HKCU:\Software\Classes\Directory{0}\shell\BashConsoleHere' -f ('', '\Background')[$_])
}
like image 79
mklement0 Avatar answered Sep 29 '22 02:09

mklement0