I learned the rudiments of shell scripting at the keyboard of a Tandy TRS-16 computer in 1982. A few years later I found The Unix Programming Environment and upgraded my knowledge significantly.
I've harped more than once that old-timers like me need to revisit manual pages even when what we know still works. Things change, new features are added and we will very often miss out on easier ways to do things if we don't keep up.
I was recently very surprised to learn that I was thirty years behind on a very basic shell feature: "here" files.
"Here" files (or "here" documents if you prefer) were mentioned in "The Unix Programming Environment" and I've used them hundreds or even thousands of times. The "bundle" example in "The Unix Programming Environment" was something I (and I'm sure many others) admired as a particularly clever piece of code. I certainly knew and used "here" files.
Here's a clip from an old Unix sh man page:
<<word The shell input is read up to a line that is
the same as word, or to an end-of-file. The
resulting document becomes the standard input.
If any character of word is quoted, no inter-
pretation is placed upon the characters of the
document; otherwise, parameter and command
substitution occurs, (unescaped) \new-line is
ignored, and \ must be used to quote the char-
acters \, $, `, and the first character of
word.
That's the "here" construct I learned. You might use it like this:
cat <<EOF This and that and the other EOF
That will just write:
This and that and the other
to your screen.
The stuff about quoting is demonstrated with this:
x="Hello" cat <<"EOF" $x EOF
That will print:
$x
while this:
x="Hello" cat <<EOF $x EOF
will print:
Hello
Any kind of quoting will work:
x="Hello" cat <<EO\F $x EOF
will also not expand $x.
Easy enough, right?
Perl has "here" files also. I use them there all the time to save typing and for neatness:
#!/usr/bin/perl print <<EOF; This and that and the other EOF
(Article continues after the break)
There's something a little unpleasant about that Perl example, isn't there? It would look a lot neater if all the stuff being printed were indented in the code (but not indented in the result). Perl can do that, but the cure is a bit ugly:
#!/usr/bin/perl
($eof= <<EOF) =~ s/^\s+//gm;
This
and that
and the other
EOF
print $eof;
That will produce the same output as the other examples.
Can the shell do that? Well, it couldn't when I learned about "here" files thirty years ago, but since then the shell man page has changed. Most importantly, something has been added:
If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line containing delimiter. This allows here-documents within shell scripts to be indented in a natural fashion.
It's not quite as flexible as the ugly Perl method. The shell version will only strip tabs, and leave spaces as they are. Of course that does let you get indents where you DO want them, at the cost of some code readability.
I never noticed that until yesterday. It's been there a long, long time but I learned all that even longer back. I do almost no shell scripting now (I use Perl), so it hardly matters, but there it is: an old dog can learn new tricks.
More Articles by Anthony Lawrence - Find me on Google+ 2012-10-09
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.
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.
I am a Kerio reseller. Articles here related to Kerio products reflect my honest opinion, but I do have an obvious interest in selling those products also.
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.
Click here to add your comments - no registration needed!
Tue Oct 9 14:59:41 2012: 11371 TonyLawrence
To make Perl act like shell, use
($eof= <<EOF) =~ s/^\t+//gm;
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