Running .sh scripts in Git Bash

后端 未结 8 2111
无人共我
无人共我 2021-01-31 01:29

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 f

相关标签:
8条回答
  • 2021-01-31 01:57

    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.

    0 讨论(0)
  • 2021-01-31 01:57
    #!/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.)

    0 讨论(0)
  • 2021-01-31 02:02

    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.

    0 讨论(0)
  • 2021-01-31 02:02

    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.

    0 讨论(0)
  • 2021-01-31 02:07

    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

    0 讨论(0)
  • 2021-01-31 02:08

    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

    0 讨论(0)
提交回复
热议问题