Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm a Mac user and new to Windows. How can I execute a shell script in Windows?

Generally, I would save the text file with the extension .sh and run it via Terminal. But how could I do that in Windows?

share|improve this question
1  
Batch files. en.wikipedia.org/wiki/Batch_file –  Kenaniah Jan 2 '12 at 5:08
1  
Can you provide an example of a script you expect to run on Windows? –  M.Babcock Jan 2 '12 at 5:10
    
Are you looking to run a pre-existing .sh shell script? Or just want to know how to script for Windows? –  Keltari May 11 '13 at 4:27
add comment

migrated from stackoverflow.com Jan 2 '12 at 5:10

This question came from our site for professional and enthusiast programmers.

7 Answers

Scripts with a .sh suffix are generally Bourne shell scripts (or bash, or ksh, or zsh -- all shells derived from the Bourne shell). (Note that on Unix-like systems, including MacOS, the .sh suffix isn't necessary; the OS looks at the #!/bin/sh line at the top of the script to determine how to execute it.)

Windows doesn't provide a Bourne-like shell.

You can install Cygwin, which provides a Unix-like environment under Windows -- but it doesn't have a particularly "native" environment. (Editorial comment: Cygwin is great for people like me who need to use Windows but would really rather be using Unix.)

There are other Unix-like subsystems that run under Windows, including MinGW and the Windows Services for UNIX package provided by Microsoft.

Or, instead of trying to write and run Unix-like shell scripts, you can write Windows batch files. These generally have a .bat or .cmd suffix. They use the same commands and syntax as the Windows interactive command prompt.

There's also the relatively new Windows PowerShell; I'm not familiar with it myself, but I've heard good things about it.

share|improve this answer
1  
I will back the powershell suggestion as a powershell user that has written several scripts for doing everything from rebooting a remote computer or shutting down services on a remote computer to pulling in a list of computer names from active directory, alphabetizing them, and testing each for certain services and doing large complicated file moves. It can literally do it ALL in windows. –  Paperlantern Jan 2 '12 at 5:31
1  
It's worth noting that while cygwin may allow the script to run, it may not run correctly if it makes some assumptions about the underlying system (ie, available binaries, filesystem layout, etc.) –  Daenyth Jan 2 '12 at 18:23
add comment

You can use Cygwin. It provides linux like environment in windows.

share|improve this answer
add comment

What about UWIN??

UWIN is a computer software package created by David Korn which allows programs written for the operating system Unix be built and run on Microsoft Windows with few, if any, changes. Some of the software development was subcontracted to Wipro, India.

Check this UWIN - Unix for Windows

Salient features of UWIN

  1. Access to almost all the command line tools from Unix on windows. 245 command line tools to be exact.
  2. Comes bundled with the original Unix compiler 'cc' as well as a plethora of tools like 'make' and the necessary libraries which allow Unix applications to be build and run on Windows machines with very little or no changes in the source code.
  3. Option to use other compilers like Visual C++ or Mingw to compile programs. Full fledged Perl package.
  4. X windows libraries for those who aspire to develop X applications on windows. Though to run those applications in Windows, you need an X server which is not bundled with UWIN.
  5. UWIN comes with a control panel applet (accessed through 'Start->Settings->Control Panel->UWIN') which can be used to configure some of the UWIN system parameters.

Uses of UWIN

  • Run Unix applications natively in Windows in full speed.
  • Use the full power of Unix command line tools on Windows.
  • The korn shell bundled with UWIN makes a Unix user feel right at home in a Windows environment.
  • Develop and run UNIX applications on Windows.
  • Develop X applications on the Windows platform.

Drawbacks of UWIN

  • UWIN does not come bundled with a X server so a user will not be able to run X applications on windows. Though there are third party commercial X servers available which can fill this gap.
  • UWIN is not released under the GPL but is free to download and use for educational and non-commercial purposes

That software was used to teach us Linux Command when i was in college.. really good one..


Ref: WikiPedia

Cygwin is a Unix-like environment and command-line interface for Microsoft Windows.

Good about is that Cygwin provides native integration of Windows-based applications, data, and other system resources with applications, software tools, and data of the Unix-like environment.

Interix
Interix is a component of the Services for Unix (SFU) release 3.0 and 3.5 (this last one is distributed freely).

The most recent releases of Interix, 5.2 and 6.0, are components of the Windows Server 2003 R2, Windows Vista Enterprise and Ultimate editions and Windows Server 2008 under the name SUA (Subsystem for Unix-based Applications) Version 6.1 is included in Windows 7 (Enterprise and Ultimate) and Windows Server 2008 R2 (all editions)

share|improve this answer
add comment

On Win the common extensions are .bat or .cmd. A good source for the commands used in Windows shell is ss64 - for CMD and PowerShell.

Cygwin is not a "must". It is just giving you the Linux style - use BASH on Win.

share|improve this answer
add comment

You'll need CygWin, and then I believe you'll be able to just run:

sh yourscript.sh
share|improve this answer
add comment

The right answer was given in a comment by Kenaniah, you have to use batch files. You need to name your shell code file to *.bat, then you can run it by clicking it or simply type its name in the terminal. Notice that the windows terminal does not work exactly like the unix terminal. You will find much help and tutorials for the commands and syntax all over the web. There is also a possibility to use the Windows PowerShell, which seems to be more unix-like.

share|improve this answer
add comment

You can use Cygwin or UWIN. You can even use FireCMD command shell which automatically installs and integrates with UWIN and also provides a terminal emulator so that Cygwin can be used in X-Terminal like environment.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.