At the start of your script you have
get rid of the ^M ...
It looks like you created the script on a windows machine and/or with a windows text editor. Is this correct? Windows end-of-line is a carriage-return followed by a line-feed. In unix, it is just a line-feed. The ^M is a carriage return. The dos2unix utility will strip these for you.