Preventing Weird Behavior in a Here-Document

Problem

Your here-document is behaving weirdly. You tried to maintain a simple list of donors using the method described previously for phone numbers. So you created a file called donors that looked like this:

$ cat donors
#
# simple lookup of our generous donors
#
grep $1 <<EOF
# name amt
pete $100
joe  $200
sam  $ 25
bill $ 9
EOF
$

But when you tried running it you got weird output:

$ ./donors bill
pete bill00
bill $  9
$ ./donors pete
pete pete00
$

Solution

Turn off the shell scripting features inside the here-document by escaping any or all of the characters in the ending marker:

# solution
grep $1 <<EOF
pete $100
joe  $200
sam  $ 25
bill $ 9
EOF

Discussion

It’s a very subtle difference, but the <<EOF is replaced with <<\EOF, or <<'EOF' or even <<E\OF—they all work. It’s not the most elegant syntax, but it’s enough to tell bash that you want to treat the “here” data differently.

Normally (i.e., unless we use this escaping syntax), says the bash man page, “…all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion.”

So what’s happening in our original donor script is that the amounts are being interpreted as shell variables. For example, $100 is being seen as the shell variable $1 followed by two zeros. That’s what gives us pete00 when we search for “pete” and bill00 when we search for “bill.”

When we escape some or all of the characters of the EOF, bash knows not to do the expansions, and the ...

Get bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.