Continuation Lines

There's been a long standing Unix convention of breaking long lines with a "\" to make them easier to read. You'd almost always see this in files like /etc/printcap, but there are plenty of other places where this convention is used.

A file with continuations might look like this:


This \
is \
one \
line.
 

and any program reading that is supposed to see it as:

This is one long line.
 

The "\" is supposed to immediately precede the line feed with no blanks, spaces or anything else between. This rigidity sometimes causes problems when folks use GUI editors to change files; they don't notice the extra spaces and subsequently programs fail.

Bash (and ksh) handle these lines by default. Using the file above as input, we can write a simple script and get one line as output:

$ while read line; do echo $line; done < t
This is one line.
$ 
 

If we use "read -r", however, it's different:

$ while read -r line; do echo $line; done < t
This \
is \
one \
line.
$ 
 

Adding spaces after each "\" produces something yet again different:

# (spaces added to file)
$ while read  line; do echo $line; done < t
This
is
one
line.
$ 
 

The missing "\"'s are because now they are simply seen as quoting the spaces. But if you use "read -r" on the same file, it's not seen as quoted:

# (same file, extra spaces after \)
$ while read -r  line; do echo $line; done < t
This \
is \
one \
line.
 

Perl

Given Perl's strong Unix roots, you might think it would recognize this convention also or at least have some funky variable you could set or unset. Nope:

$ cat t.pl
#!/usr/bin/perl
while (<>)
{
print ;
}
# (original file, no spaces after \)
$ ./t.pl < t
This \
is \
  one \
 line.
$ 
 

Section 8.1 of my Perl Cookbook presents an example program that looks for \'s, strips them and gathers full lines.

Apache groks continuation lines, and you can find stuff scattered around for more general cases: Matching line continuation characters.

Interestingly, the Perl debugger does understand backslash continuation: (from "`perldoc perldebug"):

 Multiline commands
          If you want to enter a multi-line command, such as a subroutine
          definition with several statements or a format, escape the new-
          line that would normally end the debugger command with a back-
          slash.  Here's an example:

                DB<1> for (1..4) {         \
                cont:     print "ok\n";   \
                cont: }
                ok
                ok
                ok
                ok

          Note that this business of escaping a newline is specific to
          interactive commands typed into the debugger.
 


Got something to add? Send me email.





(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Handling line continuations in Perl and shell scripts


2 comments



Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Anthony Lawrence







Wed Jun 21 14:33:07 2006: 2137   BigDumbDInosaur


For some strange reason, I've seldom used continuation characters to break up overly-long lines. I have a hunch that I got used to wrapped lines from the days when I had to punch in machine code using a Teletype (that was very long ago), where every keystroke was an exercise in shortening your finger length. Old habits do die hard. :-)



Fri May 28 01:07:03 2010: 8643   anonymous

gravatar


Using HTML to put three links on one line I tried this and it works.
< a href "www.some website.com">Try this site</a [line break]
a>< a href "www.another_website.com">Try another</a [line break]
a>< a href "www.yetanother_website.com">Or this</a [line break]
a> [line break]
HTML (at least with IE) did not complain.
The line break could not go after the < /a > because it would appear in the browser as white space.
But inside the < /a > it was ignored by the browser.
Regards

------------------------
Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





C++ is a badly designed and ugly language. It would be a shame to use it in Emacs. (Richard Stallman)

There is no reason anyone would want a computer in their home. (Ken Olson, president, chairman and founder of DEC)








This post tagged: