How To Launch Git Bash from DOS Command Line?

2019-01-16 02:16发布

I've got what I'm hoping is a simple question, but I haven't been able to find the answer yet. I would like to launch Git Bash from a DOS batch file. Here is what I tried so far:

  1. Launched Git Bash from Win 7 Start button
  2. Used CTRL+ALT+DEL to identify the process as "sh.exe"
  3. Launched sh.exe from batch file using start command

    start sh.exe
    

However, this does not launch the full Git Bash environment. Git Bash usually has "MINGW32" in the title bar, but sh.exe has a full path to ... Git\bin\sh.exe. It feels to me like there are some overlays or dependencies that I'm not aware of possibly, that also need to be loaded (pulled in? imported?).

This was one of the top results I found through searching the web, but it doesn't make complete sense to me and I'm not sure if it applies exactly to my situation:

Running git from Windows Cmd line: Where are key files?

I'm a beginner in the world of DOS batch scripting.

8条回答
三岁会撩人
2楼-- · 2019-01-16 02:46
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login -i

Git bash will get open.

查看更多
劫难
3楼-- · 2019-01-16 02:46

The answer by Endoro has aged and I'm unable to comment;

# if you want to launch from a batch file or the command line:

start "" "%ProgramFiles%\Git\bin\sh.exe" --login
查看更多
再贱就再见
4楼-- · 2019-01-16 02:47

I'm not sure exactly what you mean by "full Git Bash environment", but I get the nice prompt if I do

"C:\Program Files\Git\bin\sh.exe" --login

In PowerShell

& 'C:\Program Files\Git\bin\sh.exe' --login

The --login switch makes the shell execute the login shell startup files.

查看更多
Emotional °昔
5楼-- · 2019-01-16 02:51

I prefer to use git-bash.exe instead of sh.exe.

start "" "%ProgramFiles%\Git\git-bash.exe" -c "tail -f /c/Windows/win.ini"

You can stop closing the window when call /usr/bin/bash --login -i in the end;

start "" "%ProgramFiles%\Git\git-bash.exe" -c "echo 1 && echo 2 && /usr/bin/bash --login -i"

Note: I'm not sure this is a good way :)

查看更多
可以哭但决不认输i
6楼-- · 2019-01-16 02:56

I used the info above to help create a more permanent solution. The following will create the alias sh that you can use to open Git Bash:

echo @start "" "%PROGRAMFILES%\Git\bin\sh.exe" --login > %systemroot%\sh.bat
查看更多
啃猪蹄的小仙女
7楼-- · 2019-01-16 03:01

If you want to launch from a batch file:

  • for x86

    start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login
    
  • for x64

    start "" "%PROGRAMFILES%\Git\bin\sh.exe" --login
    
查看更多
登录 后发表回答