214

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

Run
  • 54,938
  • 169
  • 450
  • 748
  • 6
    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. – ha9u63a7 Oct 23 '14 at 07:05

11 Answers11

302

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

Faisal Mq
  • 5,036
  • 4
  • 35
  • 39
  • 1
    It doesn't work for me. A command prompt window opens then quickly closes. – desbest Apr 11 '17 at 18:13
  • 63
    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 – elshev Apr 27 '17 at 13:49
  • 2
    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` – Nam G VU Apr 09 '20 at 06:03
  • 3
    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 – Mxblsdl Jun 25 '21 at 21:23
70

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 4
    Of course, with Windows 10, you can enable WSL and almost have a real computer inside your Windows. – tripleee Aug 20 '19 at 06:25
65

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 
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • 78
    Unless you've taken special measures to get it, you're not going to have an `sh` command on Windows. – user2357112 Aug 25 '16 at 21:16
  • 1
    Ok -1 , Do you mean that i have to add a tutorial to have access to sh from the command line??? – Jorgesys Aug 25 '16 at 21:54
  • 65
    Your answer is presented as if you expect to be able to type `sh` on an arbitrary Windows command prompt and have it work. – user2357112 Aug 25 '16 at 22:04
  • msys2 is a good place to get the sh command. Check out https://github.com/userzimmermann/MSYS2-cmd if you want to use it with cmd – Aralox Sep 21 '18 at 10:13
  • 3
    Bash, and the `sh` command, is installed with Git4Windows if you select the 'Install Bash' install option. – Joel Ellis Jan 04 '19 at 20:54
  • 1
    this doesn't directly work on command prompt but works on git bash as Joel Ellis said – Oshada Jan 26 '19 at 16:53
15

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

vipmaa
  • 1,022
  • 16
  • 25
  • 3
    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`. – tripleee Dec 27 '16 at 05:31
8

Install the GitBash tool in the Windows OS. Set the below Path in the environment variables of System for the Git installation.

<Program Files in C:\>\Git\bin

<Program Files in C:\>\Git\usr\bin

Type 'sh' in cmd window to redirect into Bourne shell and run your commands in terminal.

TechRookie
  • 171
  • 2
  • 4
7

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 admin@192.168.1.1 < /mnt/c/ccode-eu.sh
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
SignedAdam
  • 71
  • 1
  • 1
5

New feature in Windows - run bash on ubuntu on windows - available in Windows 10 "Insiders" builds after the Build conference:

https://blogs.windows.com/buildingapps/2016/03/30/run-bash-on-ubuntu-on-windows/

claudiu.nicola
  • 311
  • 2
  • 9
  • 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). – m93a Oct 05 '17 at 05:49
  • Is it possible nowadays run an .sh file on Windows? I can not try it now but I'm interested in. – JesusMurF Jan 16 '18 at 08:34
  • 1
    I haven't tried it myself, but there is a guide by Microsoft on how to run sh on windows: https://learn.microsoft.com/en-us/windows/wsl/install-win10 – claudiu.nicola Jan 16 '18 at 17:03
  • @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. – tripleee Feb 19 '18 at 04:13
5

Personally I used this batch file, but it does require CygWin installed (64-bit as shown). Just associate the file type .SH with this batchfile (ExecSH.BAT in my case) and you can double-click on the .SH and it runs.

@echo off
setlocal

if not exist "%~dpn1.sh" echo Script "%~dpn1.sh" not found & goto :eof

set _CYGBIN=C:\cygwin64\bin
if not exist "%_CYGBIN%" echo Couldn't find Cygwin at "%_CYGBIN%" & goto :eof

:: Resolve ___.sh to /cygdrive based *nix path and store in %_CYGSCRIPT%
for /f "delims=" %%A in ('%_CYGBIN%\cygpath.exe "%~dpn1.sh"') do set _CYGSCRIPT=%%A
for /f "delims=" %%A in ('%_CYGBIN%\cygpath.exe "%CD%"') do set _CYGPATH=%%A

:: Throw away temporary env vars and invoke script, passing any args that were passed to us
endlocal & %_CYGBIN%\mintty.exe -e /bin/bash -l -c 'cd %_CYGPATH%;  %_CYGSCRIPT% %*'

Based on this original work.

Anonymouse
  • 935
  • 9
  • 20
5

just install git and by "bash <name.sh>" run your .sh file.

Ali Ganjbakhsh
  • 541
  • 7
  • 13
4

I use Windows 10 Bash shell aka Linux Subsystem aka Ubuntu in Windows 10 as guided here

Nam G VU
  • 33,193
  • 69
  • 233
  • 372
2

Have you tried cding to the root directory where your .sh is located in order to execute it from there, instead of writing down a path to the file as you showed in your question?

Like so:

$ cd app/build
$ build.sh
D4V1D
  • 5,805
  • 3
  • 30
  • 65