How can I launch a new Git Bash window with a specified working directory using a script (either Bash or Windows batch)?
My goal is to launch multiple Git Bash windows from a single script, each set to a different working directory. This way I can quickly get to work after booting the computer instead of having to open Git Bash windows and navigating each one to the correct working directory.
I am not asking how to change the default working directory, like this question does, but to launch one or more terminal windows with different working directories from a script.
This question is related to
bash
shell
batch-file
git-bash
Git Bash uses cmd.exe
for its terminal plus extentions from MSYS/MinGW which are provided by sh.exe
, a sort of cmd.exe
wrapper. In Windows you launch a new terminal using the start
command.
Thus a shell script which launches a new Git Bash terminal with a specific working directory is:
(cd C:/path/to/dir1 && start sh --login) &
(cd D:/path/to/dir2 && start sh --login) &
An equivalent Windows batch script is:
C:
cd \path\to\dir1
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login
D:
cd \path\to\dir2
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login
To get the same font and window size as the Git Bash launched from the start menu, it is easiest to copy the start menu shortcut settings to the command console defaults (to change defaults, open cmd.exe
, left-click the upper left icon, and select Defaults).
This is the command which can be executed directly in Run dialog box
(shortcut is win+R) and also works well saved as a .bat
script:
cmd /c (start /d "/path/to/dir" bash --login) && exit
Try the --cd= option. Assuming your GIT Bash resides in C:\Program Files\Git it would be:
"C:\Program Files\Git\git-bash.exe" --cd="e:\SomeFolder"
If used inside registry key, folder parameter can be provided with %1:
"C:\Program Files\Git\git-bash.exe" --cd="%1"
Let yet add up to the answer from @Drew Noakes:
"C:\Program Files\Git\git-bash.exe" --cd=C:\GitRepo
The cd
param should be one of the options how to specify the working directory.
Also notice, that I have not any --login
param there: Instead, I use another extra app, dedicated just for SSH keys: Pageant (PuTTY authentication agent).
C:\GitRepo
The same possible way, as @Drew Noakes mentioned/shown here sooner, I use it too.
Ctrl + Alt + B
Such shortcuts are another less known feature in Windows. But there is a restriction: To let the shortcut take effect, it must be placed somewhere on the User's subdirectory: The Desktop is fine.
If you do not want it visible, yet still activatable, place this .lnk
file i.e. to the quick launch folder, as that dir is purposed for such shortcuts. (no matter whether displayed on the desktop) #76080 #3619355
"\Application Data\Microsoft\Internet Explorer\Quick Launch\"
I might given a wrong direction, but, I like this trick.
Using Windows Explorer, pointing to any directory you want, type "cmd" in the address bar it will open Windows command prompt in that directory.
If you have the "Git" directory in your path, in the same way, you can type "git-bash" in the address bar and a Git Shell will open in that directory.
This is basically @lengxuehx's answer, but updated for Win 10, and it assumes your bash installation is from Git Bash for Windows from git's official downloads.
cmd /c (start /b "%cd%" "C:\Program Files\GitW\git-bash.exe") && exit
I ended up using this after I lost my context-menu items for Git Bash as my command to run from the registry settings. In case you're curious about that, I did this:
Bash
in the shell
key at HKEY_CLASSES_ROOT\Directory\Background\shell
Icon
(not a new key!) that is the full path to your git-bash.exe, including the git-bash.exe part. You might need to wrap this in quotes.Bash
to the text you want to use in the context menu
Bash
called command
command
's default value to cmd /c (start /b "%cd%" "C:\Program Files\GitW\git-bash.exe") && exit
Then you should be able to close the registry and start using Git Bash from anywhere that's a real directory. For example, This PC
is not a real directory.
I'm not familiar with Git Bash but assuming that it is a git shell (such as git-sh
) residing in /path/to/my/gitshell
and your favorite terminal program is called `myterm' you can script the following:
(cd dir1; myterm -e /path/to/my/gitshell) &
(cd dir2; myterm -e /path/to/my/gitshell) &
...
Note that the parameter -e
for execution may be named differently with your favorite terminal program.
Source: Stackoverflow.com