How to run .sh on Windows Command Prompt?

885,435

Solution 1

The error message indicates that you have not installed bash, or it is not in your PATH.

The top Google hit is http://win-bash.sourceforge.net/ but you also need to understand that most Bash scripts expect a Unix-like environment; so just installing Bash is probably unlikely to allow you to run a script you found on the net, unless it was specifically designed for this particular usage scenario. The usual solution to that is https://www.cygwin.com/ but there are many possible alternatives, depending on what exactly it is that you want to accomplish.

If Windows is not central to your usage scenario, installing a free OS (perhaps virtualized) might be the simplest way forward.

The second error message is due to the fact that Windows nominally accepts forward slash as a directory separator, but in this context, it is being interpreted as a switch separator. In other words, Windows parses your command line as app /build /build.sh (or, to paraphrase with Unix option conventions, app --build --build.sh). You could try app\build\build.sh but it is unlikely to work, because of the circumstances outlined above.

Solution 2

Install GIT. During installation of GIT, add GIT Bash to windows context menu by selecting its option. After installation right click in your folder select GIT Bash Here (see attached pic) and use your sh command like for example:

sh test.sh

enter image description here

Solution 3

The most common way to run a .sh file is using the sh command:

C:\>sh my-script-test.sh 

other good option is installing CygWin

in Windows the home is located in:

C:\cygwin64\home\[user]

for example i execute my my-script-test.sh file using the bash command as:

jorgesys@INT024P ~$ bash /home/[user]/my-script-test.sh 

Solution 4

you can use also cmder

Cmder is a software package created out of pure frustration over the absence of nice console emulators on Windows. It is based on amazing software, and spiced up with the Monokai color scheme and a custom prompt layout, looking sexy from the start

Screenshot

cmder.net

Solution 5

On Windows 10 Anniversary Update, it's even easier to run shell commands in/with bash on ubuntu on windows

I was trying to set my region for my x-wrt r7000 netgear router, I found the following worked for me, using bash on ubuntu on windows, you do have to enable subsystem found in windows features, and dev mode on

ssh [email protected] < /mnt/c/ccode-eu.sh
Share:
885,435

Related videos on Youtube

Run
Author by

Run

A cross-disciplinary full-stack web developer/designer.

Updated on May 28, 2021

Comments

  • Run
    Run almost 3 years

    How can I run .sh on Windows 7 Command Prompt? I always get this error when I try to run this line in it,

    app/build/build.sh
    

    error,

    'app' is not recognized...
    

    or,

    bash app/build/build.sh
    

    error,

    'bash' is not recognized...
    

    Any ideas what have I missed?

    Here the screen grab, enter image description here

    • ha9u63ar
      ha9u63ar over 9 years
      you need cygwin (possibly). You are trying to run SHELL (.sh) scripts on Windows and that's the only way I've run my .sh files on Windows.
  • user2357112
    user2357112 over 7 years
    Unless you've taken special measures to get it, you're not going to have an sh command on Windows.
  • Jorgesys
    Jorgesys over 7 years
    Ok -1 , Do you mean that i have to add a tutorial to have access to sh from the command line???
  • user2357112
    user2357112 over 7 years
    Your answer is presented as if you expect to be able to type sh on an arbitrary Windows command prompt and have it work.
  • tripleee
    tripleee over 7 years
    Though it seems to come with an optional add-on pack which includes common Unix shell commands, the base package seems to be just a replacement for cmd.
  • desbest
    desbest about 7 years
    It doesn't work for me. A command prompt window opens then quickly closes.
  • elshev
    elshev about 7 years
    If you have GIT installed, you can add to PATH system variable a path to sh.exe. In my case it was: c:\Program Files\Git\bin. Then run sh .\script.sh from PowerShell
  • m93a
    m93a over 6 years
    Cool, but how do you run a sh file with bash? If I try to do it, the window just flashes and disappears. If I drag&drop the file into an opened bash window, it says that the path wasn't found (because it uses a different convention).
  • JesusMurF
    JesusMurF over 6 years
    Is it possible nowadays run an .sh file on Windows? I can not try it now but I'm interested in.
  • claudiu.nicola
    claudiu.nicola over 6 years
    I haven't tried it myself, but there is a guide by Microsoft on how to run sh on windows: docs.microsoft.com/en-us/windows/wsl/install-win10
  • tripleee
    tripleee about 6 years
    @m93a In an Ubuntu terminal window, bash path/to/script runs the script. There is certainly also sh in Ubuntu. If the script has a proper shebang and permissions, simply path/to/script will run it with the rnterpreter specified in the shebang.
  • Aralox
    Aralox over 5 years
    msys2 is a good place to get the sh command. Check out github.com/userzimmermann/MSYS2-cmd if you want to use it with cmd
  • TamusJRoyce
    TamusJRoyce over 5 years
    May run. But it is a completely different environment.
  • Joel Ellis
    Joel Ellis over 5 years
    Bash, and the sh command, is installed with Git4Windows if you select the 'Install Bash' install option.
  • Oshada
    Oshada over 5 years
    this doesn't directly work on command prompt but works on git bash as Joel Ellis said
  • tripleee
    tripleee over 4 years
    Of course, with Windows 10, you can enable WSL and almost have a real computer inside your Windows.
  • Nam G VU
    Nam G VU about 4 years
    This works for me like a charm in Windows 10 in 2020! Though copy/paste command not working for me as it set fixed at ctrl-ins and shift-ins
  • Kevin
    Kevin over 3 years
    Git Bash comes with Git for Windows.
  • Mxblsdl
    Mxblsdl almost 3 years
    If you are using VS Code you can use the Git Bash terminal to run .sh files. Same thing as above answer but staying within VS Code