问题
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.
回答1:
Let's say you have a script script.sh
. To run it (using Git Bash), you do the following
chmod +x script.sh
./script.sh
You can change the chmod
to the executable permissions that you want. Those can be found here.
Note: The chmod
only has to be run once. Run ./script.sh
when you want to run the script.
回答2:
#!/usr/bin/env sh
this is how git bash knows a file is executable. chmod a+x
does nothing in gitbash.
回答3:
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
回答4:
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
.
回答5:
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
回答6:
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.
来源:https://stackoverflow.com/questions/36401147/running-sh-scripts-in-git-bash