Windows shortcut to run a Git Bash script
Assuming I have a test.sh script that runs a server and Git Bash installed, how do I create a Windows shortcut that I can double click to run tesh.sh in Git Bash in the foreground and allows me to see the output of the server?
Git bash is already a batch file with content similar to this :
C:\WINNT\system32\cmd.exe /c ""C:\Git\bin\sh.exe" --login -i"
If you want run (and leave running) a shell script in the context of the shell, specify it at the command line. The trick is that when the script file name is interpreted, it uses the Windows path, not the equivalent path in the sh/Git environment.
In other words, to run the file D:\temp\test.sh
in the Git shell and leave it running, create this batch file :
C:\WINNT\system32\cmd.exe /c ""C:\Git\bin\sh.exe" --login -i -- D:\temp\test.sh"
On the other hand, if you want to run a script and get your shell back, you should :
- Open the shell as is
- Edit or create
~/.profile
(tryvi ~/.profile
) - Add this line :
~/test.sh
(ajdust the path if needed)
So with a .profile
that looks like this :
echo Executing .profile
/bin/sh ~/test.sh
And test.sh
that looks like this :
echo Hello, World!
You will get this prompt :
Welcome to Git (version 1.7.11-preview20120710)
Run 'git help git' to display the help index.
Run 'git help <command>' to display help for specific commands.
Executing .profile
Hello, World!
ixe013@PARALINT01 ~
$
Other answers work, but there is a shorter solution, that fully answers the question, which was:
How to create a Windows shortcut that I can double click to run
tesh.sh
in Git Bash
The answer is: add the following command to the Target:
field of the shortcut:
"C:\Git\bin\sh.exe" -l "D:\test.sh"
Where, -l
is the short for --login
.
To better understand what this command does, consult with official GNU docs about Invoking Bash:
-l
(--login
): Make this shell act as if it had been directly invoked by login. When the shell is interactive, this is equivalent to starting a login shell withexec -l bash
. When the shell is not interactive, the login shell startup files will be executed.exec bash -l
orexec bash --login
will replace the current shell with a Bash login shell.
Also note that:
- You either need the full path to
sh.exe
or have it in yourPATH
environment variable (as others have already pointed out). - If you really need to force shell invocation in interactive mode, you can add the
-i
option - The last parameter is the path to the script that has to be executed. This path should be in Windows format.
Best solution in my opinion:
- Invokes the right shell
- No unnecessary windows
- Invokes a bash script afterwards
- Window will stay open after the script exits
Do the following:
Create a shortcut to
mintty.exe
on your desktop, for example. It is found under%installation dir%/Git/usr/bin/mintty.exe
Edit properties of the shortcut and change the target (keep the path):
"C:\Program Files\Git\usr\bin\mintty.exe" -h always /bin/bash -l -e 'D:\folder\script.sh'
Explanation of the parameters:
-h always
keeps the window open when the script finished, so the window won’t disappear while you are still reading the output (remove if you don’t need to read the output and want the window to close automatically).
-l
makes this shell act as if it had been directly invoked by login.
-e
exits immediately if a pipeline returns a non-zero status (more info).
I'd recommend to use environment variable %ComSpec%
, instead of absolute path to cmd
:
%ComSpec% /c ""C:\Program Files (x86)\Git\bin\sh.exe" --login -i"
or even just cmd
command, which is usually available from %PATH%:
cmd /c ""C:\Program Files (x86)\Git\bin\sh.exe" --login -i"
if your C:\Program Files (x86)\Git\bin
added to PATH
(which is also common solution and one of cases on TortoiseGit installing) you can use just:
cmd /c "sh --login -i"