^M magically appears on scripts when we copy a file from windows to Linux OS after modification.
To remove the ^M characters at the end of all lines in VI use below command:
The ^V is a CONTROL-V character and ^M is a CONTROL-M. When you type this, it will look like below command. In UNIX, you can escape a control character by proceeding it with a CONTROL-V.
The :%s is a basic search and replace command in VI. Above command tells VI to replace all the ^M character globally with nothing and to escape ^M we need to type ^V.