70

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
  • 28,342
  • 21
  • 95
  • 123
Edvin
  • 1,307
  • 2
  • 12
  • 21
  • 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

9 Answers9

66

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
  • 2,787
  • 1
  • 27
  • 38
intboolstring
  • 6,200
  • 4
  • 25
  • 40
  • 35
    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
  • @noonex chmod makes your program executable. – intboolstring Jan 10 '17 at 15:11
  • intboolstring on windows ? – noonex Jan 11 '17 at 07:48
  • chmod silently doesn't work on exFAT on Windows as well – noonex Jan 11 '17 at 08:19
  • @noonex Are you using Git Bash? – intboolstring Jan 11 '17 at 19:18
  • 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) – The Red Pea Apr 29 '17 at 19:59
  • 9
    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
  • 1
    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
59
#!/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
  • 2,787
  • 1
  • 27
  • 38
18446744073709551615
  • 14,600
  • 3
  • 82
  • 116
42

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
  • 1,954
  • 1
  • 14
  • 17
  • 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
  • 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
4

If your running 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 my the result on my git bash

(coffeeapp) user (master *) capstone
$ . setup.sh
 done
(coffeeapp) user (master *) capstone
$ echo $ALGORITHMS
[RS256]
(coffeeapp) user (master *) capstone
$

Check more detail in this question

DINA TAKLIT
  • 4,946
  • 7
  • 42
  • 50
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
  • 24,099
  • 26
  • 45
  • 52
Mike
  • 31
  • 1
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
  • 1,564
  • 15
  • 14
1

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

Ash
  • 5,441
  • 5
  • 15
  • 36
0

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