(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version



Continuation Lines

June 2006



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.
 

Technorati tags:


Click here to add your comments





Wed Jun 21 14:33:07 2006: Subject:   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. :-)

Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



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. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


book graphic unix and linux troubleshooting guide

My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!



 I sell and support
 Kerio Mail server




pavatar.jpg
More:
       - Basics
       - Perl
       - Shell


Unix/Linux Consultants

Skills Tests

Guest Post Here











My Favorites

Change Congress