How to run .sh on Windows Command Prompt?

WindowsBashCommand LineSh

Windows Problem Overview


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

Windows Solutions


Solution 1 - Windows

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 2 - Windows

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 3 - Windows

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 - Windows

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 - Windows

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

Solution 6 - Windows

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/

Solution 7 - Windows

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.

Solution 8 - Windows

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

> \Git\bin

> \Git\usr\bin

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

Solution 9 - Windows

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

Solution 10 - Windows

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

Solution 11 - Windows

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRunView Question on Stackoverflow
Solution 1 - WindowsFaisal MqView Answer on Stackoverflow
Solution 2 - WindowstripleeeView Answer on Stackoverflow
Solution 3 - WindowsJorgesysView Answer on Stackoverflow
Solution 4 - WindowsvipmaaView Answer on Stackoverflow
Solution 5 - WindowsSignedAdamView Answer on Stackoverflow
Solution 6 - Windowsclaudiu.nicolaView Answer on Stackoverflow
Solution 7 - WindowsAnonymouseView Answer on Stackoverflow
Solution 8 - WindowsTechRookieView Answer on Stackoverflow
Solution 9 - WindowsNam G VUView Answer on Stackoverflow
Solution 10 - WindowsAli GanjbakhshView Answer on Stackoverflow
Solution 11 - WindowsD4V1DView Answer on Stackoverflow