Why my written shell script doesn’t work in cygwin?

May 20th, 2010 mysurface Posted in Common, cygwin, dos2unix | Hits: 172192 | 2 Comments »

You may facing the problem like this. The workable shell script in Linux, after you copy and paste into windows editor and save as .sh, it doesn’t work under Cygwin shell. Why?

Bear in mind that, Cygwin shell giving you a *nix environment (either Unix or Linux), which is actually a bash environment. Everything task carry on outside the bash environment will be under the windows environment.

Line terminator between windows and Linux environment are different. When you hit enter, in windows it actually inject code CR and LF into the end of the line; where in *nix only CR is inject.

Therefore if your script was written by using windows text editor (even w32 gvim), it will gives you line terminator with CR and LF.

How to verify?
Check your written shell script with cat -v, if your script display something ^M at the end of the line, that indicates the text file is created under windows environment. And CR LF hurts the execution of bash script, ouch!

cat -v myscript.sh

How to fix it?
There is a command for you to convert windows text format into unix text format.

To convert windows text format into unix text format, do this:

dos2unix myscript.sh

Now you are ready to run the script, but remember to chmod to the script to make it executable before executing it.

chmod +x myscript.sh

2 Responses to “Why my written shell script doesn’t work in cygwin?”

  1. thanks a lot………it helped me a lot….

  2. Ashleigh Obaker Says:

    I have fun with, lead to I discovered exactly what I was taking a look for. You have ended my 4 day long hunt! God Bless you man. Have a great day. Bye

Leave a Reply