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:

Notepad2
PSPad
SciTE

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

2 comments:

  1. Philip has posted a brief follow up to Managing Line Endings over at Armadillo. He’s also got some articles which may be useful for anyone forced to use [...]

    ReplyDelete
  2. [...] and I were discussing options for Windows text editors and we realised that the list on this blog post [...]

    ReplyDelete