Windows shortcut to run a Git Bash script
Asked Answered
R

4

60

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?

Rawden answered 4/2, 2014 at 21:51 Comment(0)
R
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 ~
$
Resourceful answered 5/2, 2014 at 15:7 Comment(6)
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.Rawden
Is there a way to suppress the four lines "Welcome to Git ..." at the beginning?Headpiece
For many it'll be C:\Windows\System32\cmd.exe /c ""C:\Program Files\Git\bin\sh.exe" --login -i -- D:\temp\test.sh"Ecthyma
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.Norword
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.Norword
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.shNorword
F
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.
Futile answered 28/2, 2016 at 16:58 Comment(2)
+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!)Uncanny
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.Norword
E
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).

Ecthyma answered 9/4, 2017 at 18:54 Comment(1)
Nice. And if necessary, remember to set "Start in" - the current directory when the script runs.Iggie
H
7

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"
Hoiden answered 11/7, 2015 at 14:29 Comment(4)
I had to use cmd.exe (with the .exe extension), otherwise, it would not run.Southward
check your PATHEXT environmen varialbe, it should be something like this .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSCHoiden
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYWSouthward
If you create a shortcut file as requested by the OP, cmd.exe needn't be involved at all.Norword

© 2022 - 2024 — McMap. All rights reserved.