Bash – Start sh.exe (bash) with given path

bashcommand linegitpath

I need to start the git-bash (sh.exe) with a predefined path in a Windows environment.

For cmd.exe this can be done with a command like:

cmd.exe /K "cd /d {PATH_GOES_HERE}"

For powershell.exe this can be done with this command:

powershell.exe -noexit -command "cd '{PATH_GOES_HERE}'"

But I could not manage to get that same with git-bash aka sh.exe. I tried stuff like …

sh.exe --login -i -c "cd {PATH_GOES_HERE}"

… but I could not make it work.

The command line itself works, by entering …

sh.exe --login -i -c "ls"

… I get the bash-colorized output of the directory

enter image description here

Best Answer

Thanks to a tip from Michael D, I installed Git with the feature "Git Bash Here". Then I looked up the way Git does it by browsing the registry at:

HKEY_CLASSES_ROOT\Directory\shell\git_shell\command

That gave me the command to use:

C:\Program Files\Git\git-bash.exe "--cd=%1"

Where %1 stands for the path provided by the Windows Explorer.

So all I had to do was to go for a command like:

C:\Program Files\Git\git-bash.exe "--cd={PATH_GOES_HERE}"

Btw, no need to transform Windows paths to stuff like /c/users/myuser/...

That means I am not using sh.exe but git-bash.exe from now on.

Thanks mates.

Related Question