70

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rainman_s
  • 1,088
  • 1
  • 8
  • 19

6 Answers6

94

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.

Simon
  • 10,679
  • 1
  • 30
  • 44
  • 7
    just a reminder: set PATH=C:\cygwin64\bin;%PATH% && C:\cygwin64\bin\bash testit.sh – user861746 Mar 29 '16 at 01:50
  • 1
    When i try this, I get errors from the shell scripts commands, "mkdir: command not found". It seems like the shell script is being executed by Windows, not cygwin? – swv Apr 28 '16 at 21:39
  • 1
    @swv: That is easy to test. For example, if you put `echo -n hello` in a script `testit.sh`, when run by Cygwin the output will be `hello` and when run by the Windows command interpreter the output will be `-n hello`. I expect a more likely explanation is that the path is not being set correctly in `bash`, so I would check to see if `bash` is reading `.profile` or `.bash_profile` correctly – Simon Apr 30 '16 at 01:58
  • Hey I am getting a "FIND: Parameter format not correct" .. its not recognizing bash commands? even though I can run it in cygwin... – ScipioAfricanus Nov 23 '18 at 03:52
  • @ScipioAfricanus: You may be using the Windows `find.exe` rather than the bash version - see [Find: Parameter format not correct](https://stackoverflow.com/questions/3918341/find-parameter-format-not-correct), for example. – Simon Nov 24 '18 at 02:40
  • @user861746 I think your comment solves my old question on superuser: https://superuser.com/questions/1602846/how-to-get-correct-environment-when-launching-cygwin-script – Bowi Jun 22 '21 at 14:22
  • grep: command not found ... you *NEED* to manually put in your own script things like path but, yes, solution is working – THESorcerer Sep 02 '23 at 10:33
46

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
Roman Hocke
  • 4,137
  • 1
  • 20
  • 34
32

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/

PlunkettBoy
  • 2,915
  • 2
  • 20
  • 12
10

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.

tsaulic
  • 707
  • 1
  • 11
  • 22
4

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 "[032mGroovy[0m"

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.

mike rodent
  • 14,126
  • 11
  • 103
  • 157
3

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

david m lee
  • 2,547
  • 26
  • 14