Line Termination Character in Unix and Windows and Ctrl-M (^M) character


Wondering, why I am writing this post……!!!

It is common problem faced during development. Mostly developer work on Windows platform, create shell script file for FTP, SQL loader, etc and move that file to Unix operating system and define a concurrent program on it.

When you submit the program, it ends in error. DBAs complains that there is Ctrl-M character in the file.

What is this Ctrl-M character? Why are those in the file? How to avoid these?

Let me explain. Unix and Windows use different line termination characters. Unix use only line feed while windows use both carriage return and line feed as termination characters. This CR and LF becomes Ctrl-M in Unix.

line termination character windows and unix

Avoid Ctrl-M character

Let me tell you the way to avoid Ctrl-M character from a file.

Text editor option

We use either Notepad++ or TextPad while working on any text file. These editors generally have built-in option to save the file in Unix or Windows format.

In Notepad++, navigate to Edit->EOL Conversion and chose the respective file format.

notepad eol conversion


We use sFTP or FTP software like WinSCP to transfer files from Windows to Unix environment. This software provides options to transfer file using either Binary or ASCII mode.

Binary Mode – This mode we use to transfer binary file like form builder file .fmb, report builder file .rdf. It just moves the file as is with all properties.

ASCII Mode – This mode we use to transfer text file. It moves the file from one system to another and it also does explicit character conversion. Windows format file is automatically converted to Unix format file.

Explicit character conversion

Some Unix operating system like Solaris provides built-in command/utility to convert file from Unix to DOS and vice versa.

dos2unix dos_format.txt unix_format.txt
unix2dos unix_format.txt dos_format.txt

These are different methods to avoid Cntrl-M character. I am suggesting to follow approach 1 combined with approach 2.

Want to know more about line termination character, just head on to Wikipedia.


Please enter your comment!
Please enter your name here