Quantcast

Continuation Lines

Get the WebProNews Newsletter:


[ Business]

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.

*Originally published at APLawrence.com

Add to Del.icio.us | Digg | Yahoo! My Web | Furl

Bookmark WebProNews:

A.P. Lawrence provides SCO Unix and Linux consulting services http://www.pcunix.com

Continuation Lines
Comments Off
Top Rated White Papers and Resources

Comments are closed.

  • Join for Access to Our Exclusive Web Tools
  • Sidebar Top
  • Sidebar Middle
  • Sign Up For The Free Newsletter
  • Sidebar Bottom