Running .sh scripts in Git Bash

孤人 提交于 2019-12-02 21:41:19

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.

#!/usr/bin/env sh

this is how git bash knows a file is executable. chmod a+x does nothing in gitbash.

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
Mike

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!