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
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.
Got something to add? Send me email.
More Articles by Anthony Lawrence © 2012-10-09 Anthony Lawrence
The people I distrust most are those who want to improve our lives but have only one course of action in mind. (Frank Herbert)
Tue Oct 9 14:59:41 2012: 11371 TonyLawrence
To make Perl act like shell, use
($eof= <<EOF) =~ s/^\t+//gm;
------------------------
Printer Friendly Version
"Here" files (shell scripting) Copyright © October 2012 Tony Lawrence
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