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

'Here' files (shell scripting)

© October 2012 Anthony Lawrence


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

"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

That's the "here" construct I learned. You might use it like this:

cat <<EOF
and that
and the other

That will just write:

and that
and the other

to your screen.

The stuff about quoting is demonstrated with this:

cat <<"EOF"

That will print:


while this:

cat <<EOF

will print:


Any kind of quoting will work:

cat <<EO\F

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:

print <<EOF;
and that
and the other

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:

($eof= <<EOF) =~ s/^\s+//gm;
    and that
    and the other
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.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> 'Here' files (shell scripting)

1 comment

Inexpensive and informative Apple related e-books:

iOS 10: A Take Control Crash Course

Take Control of Upgrading to El Capitan

Take Control of iCloud, Fifth Edition

Take Control of Automating Your Mac

Take Control of iCloud

More Articles by © Anthony Lawrence

Tue Oct 9 14:59:41 2012: 11371   TonyLawrence


To make Perl act like shell, use

($eof= <<EOF) =~ s/^\t+//gm;


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

The people I distrust most are those who want to improve our lives but have only one course of action in mind. (Frank Herbert)

Linux posts

Troubleshooting posts

This post tagged:






Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode