This is because of the line endings in your file are DOS line endings. The clue is the ^M. What you need to do is just re-save the file using Unix line endings.
To fix this, open your script with vi or vim and enter in vi command mode (key ESC), then type this:
Finally save it