Start sh.exe (bash) with given path

16,404

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.

  1. start sh.exe --login
  2. create .bashrc file by entering echo "cd c/Windows/system32/" > ~/.bashrc
  3. logout (Ctrl + D)
  4. start sh.exe --login
  5. enter pwd should be c/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).

Share:
16,404

Related videos on Youtube

Waescher
Author by

Waescher

UI/UX designer, software architect and passionated C# developer

Updated on September 18, 2022

Comments

  • Waescher
    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

    enter image description here

  • Waescher
    Waescher about 7 years
    I 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.
    Michael D. about 7 years
    yeah sh -c is just running the command and exits after that.
  • Waescher
    Waescher about 7 years
    I learned that, yeah 😐
  • Michael D.
    Michael D. about 7 years
    when installing git on windows, it's gives you the git bash here with the right click in the win explorer
  • Waescher
    Waescher about 7 years
    Might 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.
    Michael D. about 7 years
    is there a reason why you need to be in that path? You could just add that path to your environment
  • Waescher
    Waescher about 7 years
    You'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
    Waescher about 7 years
    Pay attention to skip backslashes at the end: it won't accept ´C:\Users\´ but ´C:\Users´ for example