It's caused by the DOS/Windows line-ending characters. Like Andy Whitfield said, the Unix command dos2unix will help fix the problem. If you want more information, you can read the man pages for that command.
The SQL script was originally created on a Windows OS. The '^M' characters are a result of Windows and Unix having different ideas about what to use for an end-of-line character. You can use perl at the command line to fix this.
To get the ^M hold the CTRL key, press V then M (Both while holding the control key) and the ^M will appear. This will find all occurrences and replace them with nothing.
The cause is the difference between how a Windows-based based OS and a Unix based OS store the end-of-line markers.
Windows based operating systems, thanks to their DOS heritage, store an end-of-line as a pair of characters - 0x0D0A (carriage return + line feed). Unix-based operating systems just use 0x0A (a line feed). The ^M you're seeing is a visual representation of 0x0D (a carriage return).
dos2unix will help with this. You probably also need to adjust the source of the scripts to be 'Unix-friendly'.
The easiest way is to use vi. I know that sounds terrible but its simple and already installed on most UNIX environments. The ^M is a new line from Windows/DOS environment.
from the command prompt: $ vi filename
Then press ":" to get to command mode.
Search and Replace all Globally is :%s/^M//g "Press and hold control then press V then
M" which will replace ^M with nothing.
Another vi command that'll do: :%s/.$// This removes the last character of each line in the file. The drawback to this search and replace command is that it doesn't care what the last character is, so be careful not to call it twice.
As already explained, Windows programs like to terminate lines with CRLF, i.e. \r\n instead of the Unix/Linux standard \n. Since I don't need all the features of dos2unix I replaced it by adding the following to my ~/.bashrc which removes the \r: