I'm on windows 7 using Cygwin.

My script and text file are located in the same directory.

while read name; do
echo "Name read from file - $name"
done < /home/Matt/servers.txt

I get this error and I don't know why because this is correct while loop syntax..?

u0146121@U0146121-TPD-A ~/Matt
$ ./script.sh
./script.sh: line 4: syntax error near unexpected token `done'
./script.sh: line 4: `done < /home/Matt/servers.txt'

Can anybody tell me what I'm doing wrong? I think its because I'm on windows and using Cygwin.

  • It should work. can you check complete path of your home from cygwin prompt by going to /home/Matt/. please note that your home would be under C:\cygwin\home\matt. It is case sensitive when you access from cygwin.
    – Raza
    Jul 1, 2013 at 16:03
  • 2
    You have CR-LF line ending in the script. Change it to LF only (edit with vim, :set notextmode :wq) and all works.
    – ott--
    Jul 1, 2013 at 16:05
  • @ott--I'm not quite sure what you mean.
    – mkrouse
    Jul 1, 2013 at 16:07
  • @Salton Not sure what you are asking either.
    – mkrouse
    Jul 1, 2013 at 16:09
  • You can also try flip -u script.sh to remove CR LF
    – Raza
    Jul 1, 2013 at 16:15

4 Answers 4


As pointed out by ott--, your script has CR LF line endings. This is more visible with od.

$ od -c script
0000000   #   !   /   b   i   n   /   b   a   s   h  \r  \n   w   h   i
0000020   l   e       r   e   a   d       n   a   m   e   ;       d   o
0000040  \r  \n   e   c   h   o       "   N   a   m   e       r   e   a
0000060   d       f   r   o   m       f   i   l   e       -       $   n
0000100   a   m   e   "  \r  \n   d   o   n   e       <       /   h   o
0000120   m   e   /   M   a   t   t   /   s   e   r   v   e   r   s   .
0000140   t   x   t  \r  \n

As you can see, you have \r (carriage return) and \n (line feed) characters at the end of each line where you should only have \n characters. This is a result of a compatibility issue between Windows and *nix systems. Bash has difficulty dealing with the \r characters.

You can fix your script by using a utility like dos2unix or by running the following line.

sed -i 's/\r$//' script
  • That was my problem. Thanks for providing an easy way to fix it!
    – mkrouse
    Jul 2, 2013 at 14:12

This is your script with hexdump

00000000  23 21 2f 62 69 6e 2f 62  61 73 68 0d 0a 77 68 69  |#!/bin/bash..whi|
00000010  6c 65 20 72 65 61 64 20  6e 61 6d 65 3b 20 64 6f  |le read name; do|
00000020  0d 0a 65 63 68 6f 20 22  4e 61 6d 65 20 72 65 61  |..echo "Name rea|

The lines end with 0d 0a aka CR LF. Change it to 0a or LF only.


The file you're using has windows style line feeds (CR) at the end of them (same thing happened to me using Cygwin on Windows XP).

Use 'dos2unix.exe' to rectify and you should be ok:

$ dos2unix.exe script.sh

dos2unix: converting file script.sh to Unix format...

Then rerun your script and those error messages should no longer appear.


The problem is because you have the script file under a non-unix format you can use:

vi new_name_for_your_script

and do a copy-paste ( you're running over windows )

your problem will be solved.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .