{"id":1845,"date":"2014-02-12T08:35:40","date_gmt":"2014-02-12T08:35:40","guid":{"rendered":"http:\/\/blog.designed79.co.uk\/?p=1845"},"modified":"2014-02-12T08:35:40","modified_gmt":"2014-02-12T08:35:40","slug":"here-documents-preventing-weird-behavior","status":"publish","type":"post","link":"https:\/\/blog.designed79.co.uk\/?p=1845","title":{"rendered":"HERE Documents &#8211; Preventing Weird Behavior"},"content":{"rendered":"<p><span style=\"line-height: 1.5;\">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:<\/span><\/p>\n<div title=\"Page 80\">\n<div>\n<pre>     $ cat donors\r\n     #\r\n     # simple lookup of our generous donors\r\n     #\r\n     grep $1 &lt;&lt;EOF\r\n     # name amt\r\n     pete $100\r\n     joe  $200\r\n     sam  $ 25\r\n     bill $  9\r\n     EOF\r\n     $<\/pre>\n<\/div>\n<\/div>\n<div title=\"Page 81\">\n<div>\n<p>But when you tried running it you got weird output:<\/p>\n<pre>     $ .\/donors bill\r\n     pete bill00\r\n     bill $  9\r\n     $ .\/donors pete\r\n     pete pete00<\/pre>\n<p><strong>Solution<\/strong><\/p>\n<p>Turn off the shell scripting features inside the here-document by escaping any or all of the characters in the ending marker:<\/p>\n<pre>     # solution\r\n     grep $1 &lt;&lt;\\EOF\r\n     pete $100\r\n     joe  $200\r\n     sam  $ 25\r\n     bill $  9\r\n     EOF<\/pre>\n<p><strong>Discussion<\/strong><\/p>\n<p>It\u2019s a very subtle difference, but the &lt;&lt;EOF is replaced with &lt;&lt;\\EOF, or &lt;&lt;&#8216;EOF&#8217; or even &lt;&lt;E\\OF\u2014they all work. It\u2019s not the most elegant syntax, but it\u2019s enough to tell bash that you want to treat the \u201chere\u201d data differently.<\/p>\n<p>Normally (i.e., unless we use this escaping syntax), says the bash manpage, \u201c&#8230;all lines of the here-document are subjected to parameter expansion, command substi- tution, and arithmetic expansion.\u201d<\/p>\n<p>So what\u2019s happening in our original donor script is that the amounts are being inter- preted as shell variables. For example, $100 is being seen as the shell variable $1 fol- lowed by two zeros. That\u2019s what gives us pete00 when we search for \u201cpete\u201d and bill00 when we search for \u201cbill.\u201d<\/p>\n<p>When we escape some or all of the characters of the EOF, bash knows not to do the expansions, and the behavior is the expected behavior:<\/p>\n<pre>     $ .\/donors pete\r\n     pete $100\r\n     $<\/pre>\n<p>Of course you may want the shell expansion on your data\u2014it can be useful in the correct circumstances, but isn\u2019t what we want here. We\u2019ve found it to be a useful practice to always escape the marker as in &lt;&lt;&#8216;EOF&#8217; or &lt;&lt;\\EOF to avoid unexpected results, unless you know that you really want the expansion to be done on your data.<\/p>\n<\/div>\n<\/div>\n<div title=\"Page 82\">\n<p>Trailing whitespace (e.g., even just a single blank space) on your clos- ing EOF marker will cause it not to be recognized as the closing marker. bash will swallow up the rest of your script, treating it as input too, and looking for that EOF. Be sure there are no extra characters (espe- cially blanks or tabs) after the EOF.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Your here-document is behaving weirdly. You tried to maintain a simple list of donors using the method described previously for phone numbers. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1845","post","type-post","status-publish","format-standard","hentry","category-info-on-tech"],"_links":{"self":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1845","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1845"}],"version-history":[{"count":0,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1845\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1845"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1845"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}