Running shell script via Cygwin on Windows

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

+48
cygwin batch-file
Apr 01 '13 at 2:30
source share
5 answers

Sure. On my (rather vanilla) installation, Cygwin bash is in c:\cygwin\bin , so I can run the 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 entered on the command line, and with the same effect.

+68
Apr 01 '13 at 2:45
source share

One more thing - if you edited the shell script in some kind of Windows text editor that creates the line endings \r\n , cygwin bash will not accept those \r . Just run dos2unix testit.sh before running the script:

 C:\cygwin\bin\dos2unix testit.sh C:\cygwin\bin\bash testit.sh 
+32
Sep 12 '13 at 19:15
source share

If you have access to the Notepad ++ editor on Windows, there is a feature that makes it easy to work around this problem:

  • Open the file reporting the error in Notepad ++.
  • Go to the "Edit" menu and select "EOL Conversion"
  • There is an option "UNIX / OSX format". Select this option.
  • Save the file.

I did this and he solved my problems.

Hope this helps!

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

+18
Feb 27 '14 at 16:04
source share

I just wanted to add that you can do this in order to apply the dos2unix solution to all the files under the directory, since it saved me a lot of time when we had to “fix” a bunch of our scripts.

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

I would do this as a comment on the Roman answer, but I still do not have access to the comments.

+8
Mar 14 '14 at 0:40
source share

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


To illustrate:
1. Always point .sh to your script file name, for example, test1.sh
2. test1.sh is as follows:
#!/bin/bash echo '$0 = ' $0 echo '$1 = ' $1 filepath=$1 > 3. Windows Cygwin "test1.sh"
4. Unix "test1.sh"


Note. On Windows, you need to use the file explorer to do the following:
1. Open the file explorer
2. Right-click the file with the extension .sh, for example test1.sh
3. Open with ... → Select sh.exe
After that, your Windows 10 remembers the execution of all .sh files using sh.exe.


Note. Using this method, you do not need to add the script file name in bash to run

+1
Dec 19 '15 at 20:08
source share



All Articles