Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running .sh scripts in Git Bash

I'm on a Windows machine using Git 2.7.2.windows.1 with MinGW 64.

I have a script in C:/path/to/scripts/myScript.sh.

How do I execute this script from my Git Bash instance?

It was possible to add it to the .bashrc file and then just execute the entire bashrc file.

But I want to add the script to a separate file and execute it from there.

like image 496
Edvin Avatar asked Apr 04 '16 11:04

Edvin


People also ask

Can I run an sh script in Bash?

In order to run a Bash script on your system, you have to use the “bash” command and specify the script name that you want to execute, with optional arguments. Alternatively, you can use “sh” if your distribution has the sh utility installed. As an example, let's say that you want to run a Bash script named “script”.

Can we do shell scripting in Git Bash?

Launch Git Bash It includes the complete set of Git core commands. It is also packaged with additional commands which can be found in the /usr/bin directory of the Git Bash emulation. This is how we can use Bash Shell Scripting on Windows operating system.


3 Answers

Let's say you have a script script.sh. To run it (using Git Bash), you do the following: [a] Add a "sh-bang" line on the first line (e.g. #!/bin/bash) and then [b]:

# Use ./ (or any valid dir spec):
./script.sh

Note: chmod +x does nothing to a script's executability on Git Bash. It won't hurt to run it, but it won't accomplish anything either.

like image 60
intboolstring Avatar answered Oct 21 '22 19:10

intboolstring


#!/usr/bin/env sh

this is how git bash knows a file is executable. chmod a+x does nothing in gitbash. (Note: any "she-bang" will work, e.g. #!/bin/bash, etc.)

like image 33
18446744073709551615 Avatar answered Oct 21 '22 18:10

18446744073709551615


If you wish to execute a script file from the git bash prompt on Windows, just precede the script file with sh

sh my_awesome_script.sh
like image 54
JohnWrensby Avatar answered Oct 21 '22 18:10

JohnWrensby