Remove Control-M In Your Unix Script

Another tip to document:

I was once a victim of wanting to run my shell script only to realize one of the reasons why I can’t run it is because of the presence of Control-M character all over my script!

Why is it so?

The cause of such Control-M presence is due to the fact that the script was created using Windows based Notepad or any non-Unix editor. Now, the scripts have to be uploaded using FTP or SFTP to the target Unix server.

Currently there are largely available GUI based tool to perform the FTP or SFTP. But one thing isn’t clear is when you FTP in your scripts, you have ensure that the mode is in ASCII. If you missed it out, you will end up having Control-M characters in your scripts.

Back to the business of removing the Control-M character, just type this command using your ‘vi’ editor. Follow the key strokes:


After pressing the key strokes, you will see this in your ‘vi’ editor:


Now you can review your script and satisfied that the Control-M character is gone.

For safety, before even editing your shell script, you can back it up first using the cp (copy) command:

cp -p original_file backup_file

This command will copy the file to your target backup_file with the same permission as your original_file.

Til next time. Au revoir.