APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Continuation Lines


© June 2006 Anthony Lawrence

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


Inexpensive and informative Apple related e-books:

Take Control of Apple Mail, Third Edition

Take Control of Automating Your Mac

Take Control of OS X Server

Are Your Bits Flipped?

Photos for Mac: A Take Control Crash Course




More Articles by © 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

------------------------


Printer Friendly Version

Have you tried Searching this site?

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


Printer Friendly Version





I am not the only person who uses his computer mainly for the purpose of diddling with his computer. (Dave Barry)




Linux posts

Troubleshooting posts


This post tagged:

Basics

Perl

Scripting

Shell



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode