Sunday, May 20, 2007

Managing Line Endings

Moments ago I was asked to help figure out why a simple bash script was producing a "Bad interpreter" error on a Linux box. After checking that the bash interpreter was indeed where the script believed it was, I deduced that the problem was none other than the invisible gremlin that has plagued electronic text for over thirty years: line endings.

Briefly put, Unix lines (including Linux and Mac OS X) end with a line feed character (LF), while Windows lines end with two characters: a carriage return and a line feed (CR+LF). Not surprisingly, moving text between these systems can lead to interesting and annoying problems.

There are many ways to deal with this problem, a Google search will turn up command line fixes using perl, sed, awk, and tr; there are also command-line utilities such as dos2unix and unix2dos; however, if this is a recurring problem, a better text editor is usually called for. The following are links to text editors with support for either viewing or explicitly choosing line endings. No attempt has been made to rate them, as they all pale vis-a-vis God's Own Editor, but desirable features may include syntax highlighting, auto indentation, line numbering/jumping, brace matching, and of course Unix/Mac/Windows conversion:


NB: The above are all Windows programs as most Mac & Linux editors are line ending aware.