Start sh.exe (bash) with given path
Solution 1
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.
Solution 2
bash
is reading the .bashrc
file on start.
- start
sh.exe --login
- create
.bashrc
file by enteringecho "cd c/Windows/system32/" > ~/.bashrc
- logout (Ctrl + D)
- start
sh.exe --login
- enter
pwd
should bec/Windows/system32/
AFAIK windows is protecting the \Program Files\
folder with the UAC. You might need to run sh.exe
as admin, or consider to install git to C:\git
instead (outside the program files folder).
Related videos on Youtube
Waescher
UI/UX designer, software architect and passionated C# developer
Updated on September 18, 2022Comments
-
Waescher over 1 year
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
-
Waescher about 7 yearsI really appreciate that answer, but that seems not to be a reasonable way to go for me, I'm afraid. I don't have administrative priviledges and I don't want to edit configuration files just to init my bash session.
-
Michael D. about 7 yearsyeah
sh -c
is just running the command and exits after that. -
Waescher about 7 yearsI learned that, yeah 😐
-
Michael D. about 7 yearswhen installing git on windows, it's gives you the
git bash here
with the right click in the win explorer -
Waescher about 7 yearsMight be, but I cannot use it like that, sorry. However, it would be interesting how this is done internally. Might take a look, thank you!
-
Michael D. about 7 yearsis there a reason why you need to be in that path? You could just add that path to your environment
-
Waescher about 7 yearsYou're the man - that tip was pure gold! But I think I'll go for my own answer, so that the next readers will see it. Thanks!
-
Waescher about 7 yearsPay attention to skip backslashes at the end: it won't accept ´C:\Users\´ but ´C:\Users´ for example