115

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Edvin
  • 1,841
  • 4
  • 16
  • 23
  • Possible duplicate of *[Windows shortcut to run a Git Bash script](https://stackoverflow.com/questions/21564275/windows-shortcut-to-run-a-git-bash-script)* – Peter Mortensen Aug 16 '19 at 17:18

10 Answers10

112

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.

Stabledog
  • 3,110
  • 2
  • 32
  • 43
intboolstring
  • 6,891
  • 5
  • 30
  • 44
  • 41
    that obviously is not valid answer for windows, as chmod really silently does nothing here, (at least for me) – noonex Jan 10 '17 at 08:51
  • 1
    @noonex chmod makes your program executable. – intboolstring Jan 10 '17 at 15:11
  • intboolstring on windows ? – noonex Jan 11 '17 at 07:48
  • 12
    The significant part of this answer was the use of `./`, (i.e. I did not need to run the `chmod` command). The `./...` [is explained here](http://stackoverflow.com/q/6331075/1175496) – Nate Anderson Apr 29 '17 at 19:59
  • 10
    I thought this wasn't working on Git Bash on Windows 10, but it's just that autocomplete wasn't working. When I manually typed out the whole file name (e.g. `./my_really_complicated_long_script_name_that_I_wish_were_autocompletable.sh`), it worked. Remember to start with `./`. – Ryan May 17 '17 at 19:29
  • 5
    Having tested this quite a bit, my conclusion is that [`chmod +x [script]` on git-bash (for Windows) does **nothing whatsoever**](https://stackoverflow.com/a/44884649/237059). Git bash is looking for the sh-bang (#!/[interpreter]), and that's what it uses to assess executablity. As such, the answer here is actually just wrong, except for the part which requires a `./` prefix, which is true for unix in general. – Stabledog Jan 10 '20 at 00:38
71
#!/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.)

Stabledog
  • 3,110
  • 2
  • 32
  • 43
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
  • 1
    This works on git-bash on windows, the chmod +x seems to perform a no-op – Andrew Aug 31 '17 at 08:33
  • Doing this allows for auto complete which is the problem I was having. – OozeMeister Aug 10 '18 at 16:39
  • 2
    Add that you have to add this as first line of the script, and that this `#!` is called a shebang, which is a kernel convention interpreted by the shell. – jaques-sam Apr 24 '19 at 08:26
  • This should be the accepted answer. The other one doesn't actually work. – Stuporman Dec 12 '19 at 00:54
  • In Windows Git Bash, all you need is "./", so that other answer is indeed the right one to follow. In my limited experience, it's the only thing that does work when you install Git Bash on Windows. – Maxx Mar 01 '22 at 19:19
58

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
JohnWrensby
  • 2,652
  • 1
  • 18
  • 18
  • 0 down vote I tried every suggestion here and none of it works. I simply want to create a script that changes to a directory. No matter what I try it doesn't work. Tried to make it a .bat file, it shows the commands, but doesn't change the directory. Tried executing it with sh and it doesn't do anything. Nothing ever gives an error, it just doesn't work. – Tim Eckel Sep 05 '17 at 14:47
  • 2
    Not sure about windows, but inside of a shell, a script can not change the directory. Well, while the script is running, every "cd dir" *inside of the script* changes the working directory, obviously. But when the script is finished, the outer shell from there you started is still in the same original directory. For changing directories you would need to "source" the script with the dot command: ". script.sh", but usually it is more convenient to write an alias!! – Angel O'Sphere Jun 27 '19 at 13:33
  • 1
    I use macOS and share code with windows users. The `sh my_awesome_script.sh` works great for both of us either as a `pre-push` hook (using husky). Thanks! – naorz Apr 07 '22 at 07:38
7

if you are on Linux or ubuntu write ./file_name.sh and you are on windows just write sh before file name like that sh file_name.sh

  1. For Linux -> ./filename.sh
  2. For Windows -> sh file_name.sh
It's Diab
  • 71
  • 1
  • 4
6

If you're running an export command in your bash script, the above-given solution may not export anything even if it will run the script. As an alternative for that, you can run your script using

. script.sh 

Now if you try to echo your var it will be shown. Check the result on git bash -

$ . setup.sh
$ echo $ALGORITHMS
[RS256]

Check more detail in this question

Brian Burns
  • 20,575
  • 8
  • 83
  • 77
DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74
3

I had a similar problem, but I was getting an error message

cannot execute binary file

I discovered that the filename contained non-ASCII characters. When those were fixed, the script ran fine with ./script.sh.

zx485
  • 28,498
  • 28
  • 50
  • 59
Mike
  • 31
  • 1
3

Once you're in the directory, just run it as ./myScript.sh

Ash
  • 5,786
  • 5
  • 22
  • 42
2

If by any chance you've changed the default open for .sh files to a text editor like I had, you can just "bash .\yourscript.sh", provided you have git bash installed and in path.

taavi
  • 31
  • 1
1

I was having two .sh scripts to start and stop the digital ocean servers that I wanted to run from the Windows 10. What I did is:

  • downloaded "Git for Windows" (from https://git-scm.com/download/win).
  • installed Git
  • to execute the .sh script just double-clicked the script file it started the execution of the script.

Now to run the script each time I just double-click the script

Yogesh Awdhut Gadade
  • 2,498
  • 24
  • 19
0

#!/bin/bash at the top of the file automatically makes the .sh file executable. I agree the chmod does not do anything but the above line solves the problem. you can either give the entire path in gitbash to execute it or add it in the PATH variable export PATH=$PATH:/path/to/the/script then you an run it from anywhere

kedar
  • 41
  • 2