57

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
konyak
  • 10,818
  • 4
  • 59
  • 65

4 Answers4

70

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 :

  1. Open the shell as is
  2. Edit or create ~/.profile (try vi ~/.profile)
  3. 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 ~
$
ixe013
  • 9,559
  • 3
  • 46
  • 77
  • Awesome, after I revised the question, I realized I didn't need it to return to the Gish Bash shell prompt after executing the script. – konyak Feb 05 '14 at 15:56
  • Is there a way to suppress the four lines "Welcome to Git ..." at the beginning? – BillyTom May 31 '16 at 06:50
  • 1
    For many it'll be `C:\Windows\System32\cmd.exe /c ""C:\Program Files\Git\bin\sh.exe" --login -i -- D:\temp\test.sh"` – Hannes Schneidermayer Apr 09 '17 at 18:01
  • 1
    Using `-i` while also specifying a script to execute is mostly pointless - the `bash` instance will _not_ stay open, and to the script it will look like it's being run in an interactive shell, which is irrelevant at best, and can result in different behavior at worst. You should make it clearer that your profile-based solution will invoke the script for _all_ future login shells, which may be undesired. – mklement0 Apr 09 '17 at 21:05
  • Also, by running the script in a regular console window that defaults to the Windows OEM code page, you may run into character encoding issues. – mklement0 Apr 09 '17 at 21:16
  • 5
    Leaving the other issues aside, there is no need for an intermediate batch file; a _shortcut file_, as requested by the OP, can use this command directly (`cmd.exe` needn't be involved): `"C:\Program Files\Git\bin\sh.exe" -l D:\temp\test.sh` – mklement0 Apr 09 '17 at 21:21
36

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"

enter image description here

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 with exec -l bash. When the shell is not interactive, the login shell startup files will be executed. exec bash -l or exec 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 your PATH 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.
quasoft
  • 5,291
  • 1
  • 33
  • 37
  • 3
    +1 Perfect. Just one additional advice: Save your \*.sh script as UTF-8 (**no** `Windows 1252` like notepad does and **no** `UTF8-BOM` like Visual Studio does!) – Marcel Mar 17 '17 at 14:58
  • 3
    Note that this will automatically close the window when the script terminates, so your answer doesn't satisfy the "allows me to see the output of the server" requirement. Also, by running the script in a regular console window that defaults to the Windows OEM code page, you may run into character encoding issues. – mklement0 Apr 09 '17 at 21:15
11

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:

  1. Create a shortcut to mintty.exe on your desktop, for example. It is found under %installation dir%/Git/usr/bin/mintty.exe

  2. 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).

Hannes Schneidermayer
  • 4,729
  • 2
  • 28
  • 32
  • 1
    Nice. And if necessary, remember to set "Start in" - the current directory when the script runs. – Qwertie Jun 06 '19 at 17:04
6

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"
radistao
  • 14,889
  • 11
  • 66
  • 92
  • I had to use cmd.exe (with the .exe extension), otherwise, it would not run. – Jon Apr 14 '16 at 13:32
  • check your PATHEXT environmen varialbe, it should be something like this .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC – radistao Apr 14 '16 at 21:01
  • `PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW` – Jon Apr 15 '16 at 20:39
  • If you create a _shortcut file_ as requested by the OP, `cmd.exe` needn't be involved at all. – mklement0 Apr 09 '17 at 21:18