Running a shell script through Cygwin on Windows

Batch FileCygwin

Batch File Problem Overview


I have a bunch of shell scripts that used to run on a Linux machine. Now, we've switched over to Windows, and I need to run these scripts there. I have Cygwin installed, but is there a way to make the script run using Cygwin, but the call is made from Windows batch?

Batch File Solutions


Solution 1 - Batch File

Sure. On my (pretty vanilla) Cygwin setup, bash is in c:\cygwin\bin so I can run a bash script (say testit.sh) from a Windows batch file using a command like:

C:\cygwin\bin\bash testit.sh

... which can be included in a .bat file as easily as it can be typed at the command line, and with the same effect.

Solution 2 - Batch File

One more thing - if You edited the shell script in some Windows text editor, which produces the \r\n line-endings, cygwin's bash wouldn't accept those \r. Just run dos2unix testit.sh before executing the script:

C:\cygwin\bin\dos2unix testit.sh
C:\cygwin\bin\bash testit.sh

Solution 3 - Batch File

If you have access to the Notepad++ editor on Windows there is a feature that allows you to easily get around this problem:

  1. Open the file that's giving the error in Notepad++.
  2. Go under the "Edit" Menu and choose "EOL Conversion"
  3. There is an option there for "UNIX/OSX Format." Choose that option.
  4. Re-save the file.

I did this and it solved my problems.

Hope this helps!

Read more at http://danieladeniji.wordpress.com/2013/03/07/microsoft-windows-cygwin-error-r-command-not-found/

Solution 4 - Batch File

Just wanted to add that you can do this to apply dos2unix fix for all files under a directory, as it saved me heaps of time when we had to 'fix' a bunch of our scripts.

find . -type f -exec dos2unix.exe {} \;

I'd do it as a comment to Roman's answer, but I don't have access to commenting yet.

Solution 5 - Batch File

The existing answers all seem to run this script in a DOS console window.

This may be acceptable, but for example means that colour codes (changing text colour) don't work but instead get printed out as they are:

> there is no item "Groovy"

I found this solution some time ago, so I'm not sure whether mintty.exe is a standard Cygwin utility or whether you have to run the setup program to get it, but I run like this:

D:\apps\cygwin64\bin\mintty.exe -i /Cygwin-Terminal.ico  bash.exe .\myShellScript.sh

... this causes the script to run in a Cygwin BASH console instead of a Windows DOS console.

Solution 6 - Batch File

If you don't mind always including .sh on the script file name, then you can keep the same script for Cygwin and Unix (Macbook).

To illustrate:

  1. Always include .sh to your script file name, e.g., test1.sh
  2. test1.sh looks like the following as an example:
    #!/bin/bash echo '$0 = ' $0 echo '$1 = ' $1 filepath=$1
  3. On Windows with Cygwin, you type "test1.sh" to run
  4. On a Unix, you also type "test1.sh" to run


Note: On Windows, you need to use the file explorer to do following once:

  1. Open the file explorer
  2. Right-click on a file with .sh extension, like test1.sh
  3. Open with... -> Select sh.exe
    After this, your Windows 10 remembers to execute all .sh files with sh.exe.

    Note: Using this method, you do not need to prepend your script file name with bash to run

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
Questionrainman_sView Question on Stackoverflow
Solution 1 - Batch FileSimonView Answer on Stackoverflow
Solution 2 - Batch FileRoman HockeView Answer on Stackoverflow
Solution 3 - Batch FilejsealsView Answer on Stackoverflow
Solution 4 - Batch FiletsaulicView Answer on Stackoverflow
Solution 5 - Batch Filemike rodentView Answer on Stackoverflow
Solution 6 - Batch Filedavid m leeView Answer on Stackoverflow