How to run .sh on Windows Command Prompt?
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
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
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
Related videos on Youtube
Comments
-
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,
-
ha9u63ar over 9 yearsyou 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 over 7 yearsUnless you've taken special measures to get it, you're not going to have an
sh
command on Windows. -
Jorgesys over 7 yearsOk -1 , Do you mean that i have to add a tutorial to have access to sh from the command line???
-
user2357112 over 7 yearsYour answer is presented as if you expect to be able to type
sh
on an arbitrary Windows command prompt and have it work. -
tripleee over 7 yearsThough 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 about 7 yearsIt doesn't work for me. A command prompt window opens then quickly closes.
-
elshev about 7 yearsIf 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 runsh .\script.sh
from PowerShell -
m93a over 6 yearsCool, but how do you run a
sh
file withbash
? 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 over 6 yearsIs it possible nowadays run an .sh file on Windows? I can not try it now but I'm interested in.
-
claudiu.nicola over 6 yearsI 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 about 6 years@m93a In an Ubuntu terminal window,
bash path/to/script
runs the script. There is certainly alsosh
in Ubuntu. If the script has a proper shebang and permissions, simplypath/to/script
will run it with the rnterpreter specified in the shebang. -
Aralox over 5 yearsmsys2 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 over 5 yearsMay run. But it is a completely different environment.
-
Joel Ellis over 5 yearsBash, and the
sh
command, is installed with Git4Windows if you select the 'Install Bash' install option. -
Oshada over 5 yearsthis doesn't directly work on command prompt but works on git bash as Joel Ellis said
-
tripleee over 4 yearsOf course, with Windows 10, you can enable WSL and almost have a real computer inside your Windows.
-
Nam G VU about 4 yearsThis 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
andshift-ins
-
Kevin over 3 yearsGit Bash comes with Git for Windows.
-
Mxblsdl almost 3 yearsIf 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