How to execute .sh file on Windows?

68

15

When I am trying to execute a file(name.sh) in the command line by the command ./name.sh , I am getting the error that:

"." is not recognized as an internal or external command, operable or batch file

please help me execute the .sh file

sushant

Posted 2010-03-15T07:34:42.150

Reputation:

Answers

76

You're on Windows CMD.EXE (from the error message). It uses a different syntax to execute commands. You'll need to use sh name.sh, assuming that you've got Cygwin or similar installed.

To clarify, Windows does not have a built-in utility to support .sh files. To run such, you'll need to install a third-party tool such as Cygwin.

Roger Lipscombe

Posted 2010-03-15T07:34:42.150

Reputation: 1 933

7Could you also not just install Git on Windows? It seems that doing so adds support for shell scripts even if those shell scripts are not performing Git-related tasks. – DaveTheMinion – 2014-12-12T01:31:33.297

2@DavidB Yes, that works if you put C:\Program Files (x86)\Git\bin (or whatever it is on your computer) in your PATH, because Git places all sorts of useful tools there including sh.exe – Sumyrda - Reinstate Monica – 2016-06-23T13:55:31.057

1@DavidB Yes, Git Bash knows what to do with it, correct! – Legends – 2018-05-23T20:10:49.990

13

You are trying to run a Linux command at the Windows Command Prompt.

On Linux the forward slash is a path separater. On Windows the backslash is a path separator and the forward slash generally indicates an argument.

Therefore, Windows thinks you are trying to run a command called "." and parsing it the argument "/name.sh". The correct convention would be ".\name.sh". Additionally Windows will automatically search the current directory for your command so you could just type "name.sh".

The next problem you will face is that Windows does not know what a sh script is, again this is a Linux thing. You could solve this by installing Cygwin if you really want or need to run a sh script.

However, judging by one of your previous comments you could just as well rename the script to name.bat and delete the "#!/bin/sh" line. Now you have a Batch file which Windows should understand. You can read more about batch files here.

Martin Fido

Posted 2010-03-15T07:34:42.150

Reputation: 291

11

Alternatively you could turn to the dark side and install Linux. From the prompt:

sh runide.sh

or

sudo sh runide.sh

Your program will run and you will have a better system as well.

steve

Posted 2010-03-15T07:34:42.150

Reputation: 119

16That's like saying, " Just get rich and pay someone to do it for you " – Funkodebat – 2015-09-16T00:52:26.027

9

Best solution in my opinion:

  • Download and install .git for Windows

  • Right click desktop and say "git bash here"

  • Execute your script like in unix

  • Done!

Caution: Many commands won’t work on windows! But still, a lot of the basic stuff will work.

If you need that script regularly you may want to create a shortcut (on your desktop e.g.):

  • Create a Shortcut to mintty.exe on your desktop

  • Edit properties of the shortcut and change the target (keep the path):

  • C:\Program Files\Git\usr\bin\mintty.exe" -h always /bin/bash -l -e 'D:\folder\script.sh'

Hannes Schneidermayer

Posted 2010-03-15T07:34:42.150

Reputation: 191

This doesnt work. When I run @FOR /f "tokens=*" %i IN ('docker-machine env --shell cmd default') DO @%i. I get unexpected token ( – Steve Tomlin – 2018-10-19T12:18:03.430

5

You're trying to run your car on orange juice instead of gasoline. Windows shares similar commands stored in .bat or .cmd files with Unix/Linux/zOS Unix Subsystem/*ix shell scripts as these two families of operating systems share a common ancestor the DEC PDP-x machines.

If you want instant gratification, you will need to install an environment that provides a "sh.exe" program or "csh.exe" or "bash.exe" program (tsh.exe anyone?)

Alternatively, if you know Unix script commands, very well, and you know Windows .cmd and .bat file commands, very well, you can translate the .sh file into a .bat or .cmd file. Even so, you will often encounter more Unix-styled programs that have no equivalent under Windows--grep, sed, vi, emacs, etc. Thus, the call to install CygWin (no minimalist)--just to get the shell and Unix tools. Put it on a flash-drive, for these special occasions.

J.M.

Posted 2010-03-15T07:34:42.150

Reputation: 51

3

Someone said something about MS-DOS using / for commands and \ for paths. This is slightly misleading. Look at my example:

C:\myfoo\foo\>cd../.. 
C:\myfoo\foo\>cd..\..

Has no difference in effect. Yes, it is not true for all operations – the actual answer is simply No, or without Cygwin or SSHD you can't.

I only know because I stupidly spent half a day trying to figure out what if then fi and eval, exec do with -Djava.something when called.

grizzleybear123

Posted 2010-03-15T07:34:42.150

Reputation: 31

2

Use Github for Windows. It contains all the Unix environment executables.

https://github-windows.s3.amazonaws.com/GitHubSetup.exe

Nederealm

Posted 2010-03-15T07:34:42.150

Reputation: 121

2More specifically, install Git. The only reason installing Github for Windows contains a Bash shell is because it comes with Git. :) – ZaLiTHkA – 2015-11-19T00:37:20.270

Good alternative, thanks, so do not need to be installing more tools. +1 – Guilherme Nascimento – 2016-05-03T14:50:45.053

1

For completeness I want to mention a new “Windows Subsystem for Linux” feature for Windows 10. The details are described in http://www.howtogeek.com/249966/how-to-install-and-use-the-linux-bash-shell-on-windows-10/

Note, that it requires activate the “Developer Mode” switch and enable the “Windows Subsystem for Linux (Beta)” option.

For myself I found that Git for Windows installation (that I am using anyway) is sufficient.

MiFreidgeim SO-stop being evil

Posted 2010-03-15T07:34:42.150

Reputation: 159

1

This is an old thread but for those running Windows 10. Just open windows features from the control panel 'Turn Windows Features on or Off'

Choose the feature 'Windows Subsystem for Linux'. After installing, you have 'Bash' and you even can configure native Linux servers within bash. And of course, the sh command, within bash, works too.

Egbert

Posted 2010-03-15T07:34:42.150

Reputation: 11

1

I found a different and easy solution, called UnixUtil.

Download and unzip it to C: drive. Set the environment variable path to include C:\UnixUtils\usr\local\wbin.

This is important. DO NOT set path to C:\UnixUtils\bin

prabhanjan deshpande

Posted 2010-03-15T07:34:42.150

Reputation: 11

0

To run the shell script from the windows. First use the command : dos2unix then you can use your normal command : sh runide.sh

This will work out.

Sridhar GV

Posted 2010-03-15T07:34:42.150

Reputation: 1

Sorry, slight correction needs to be done. Use the command : "dos2unix runide.sh"

Output: dos2unix: converting file runide.sh to UNIX format

then you use your command: "sh runide.sh"

This will surely run you shell script.

Happy learning – Sridhar GV – 2015-11-23T12:02:41.800

question was not about file format, but rather about lack of shell in windows. – Archemar – 2015-11-23T13:48:13.677

0

Although the existing answers here were correct at the time, Windows 10 does now optionally include a modified version of Ubuntu running a full bash shell.

Once made executable, scripts can be run in the usual way from within bash, eg:

chmod +x myscript.sh
./myscript.sh

If you want to run a script directly from the Windows you could create a shortcut in the same directory and set the 'Target' to something like:

C:\Windows\System32\bash.exe -c "./myscript.sh"

If the path of your script is relative to the location of the shortcut (like the example above) ensure the 'Start in' field is also blank.

Molomby

Posted 2010-03-15T07:34:42.150

Reputation: 1 486

0

It's possible that the problem is actually within the file name.sh -- you are trying to use the . command but it is attempting to run a kind of shell (e.g., csh, I think) in which that's not a valid command.

So: does name.sh start with the correct #!/bin/sh (if you are actually trying to run sh)?

Andrew Jaffe

Posted 2010-03-15T07:34:42.150

Reputation: 149

1yes it does start #!/bin/sh cd D:\Raghu D: winzip32.exe -min -a -r D:\p thats the content of the file – None – 2010-03-15T08:12:08.470

0

ok, there's a LOT wrong with that - drop the #!/bin/sh, and change the extention to bat, and it might work with a few more fixes. Then you can just invoke it by its filename as well.

The syntax of a unix shell script, and a windows batch file arn't too similar. cygwin, or a load of messing around with unxutils might make something that would work in both, but unless you're ABSOLUTELY sure the environment is always the same, it isn't worth the headache

Journeyman Geek

Posted 2010-03-15T07:34:42.150

Reputation: 119 122

drop the #!/bin/sh, and change the extention to bat, and it might work with a few more fixes. What?! I suspect you never tried this... On Windows, even the simpliest bash capabilities are unsupported or require lengthy syntax. – Camilo Martin – 2012-08-31T14:29:38.297

The second paragraph covers the fact that its likely to not work, without a fair amount of work. – Journeyman Geek – 2012-08-31T15:09:17.290

Well, you're right. – Camilo Martin – 2012-08-31T15:11:10.357

0

If you have a Linux box in your home (or work) and plugged to the same network, and this network is safe, this might do the trick:

  1. Make a folder share on Windows (pretty easy, but make sure the network is safe from intruders).
  2. Mount it in Linux with mount //WinMachine/Share LinuxFolder (IIRC, and requires Samba).
  3. Using something like PuTTY, log in remotely to that Linux box.
  4. Run your command in the mounted folder.

Camilo Martin

Posted 2010-03-15T07:34:42.150

Reputation: 2 308