{"id":216218,"date":"2021-03-31T15:00:05","date_gmt":"2021-03-31T12:00:05","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-use-here-documents-in-bash-on-linux\/"},"modified":"2021-03-31T15:00:05","modified_gmt":"2021-03-31T12:00:05","slug":"how-to-use-here-documents-in-bash-on-linux","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-use-here-documents-in-bash-on-linux\/","title":{"rendered":"#How to Use \u201cHere Documents\u201d in Bash on Linux"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_89 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6af7aad6028cd\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6af7aad6028cd\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-here-documents-in-bash-on-linux\/#Here_Documents\" >Here Documents<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-here-documents-in-bash-on-linux\/#Basic_Principles_of_Here_Documents\" >Basic Principles of Here Documents<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-here-documents-in-bash-on-linux\/#Simple_Examples\" >Simple Examples<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-here-documents-in-bash-on-linux\/#Handling_Tab_Characters\" >Handling Tab Characters<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-here-documents-in-bash-on-linux\/#Redirecting_to_a_File\" >Redirecting to a File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-here-documents-in-bash-on-linux\/#Piping_the_Output_to_Another_Command\" >Piping the Output to Another Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-here-documents-in-bash-on-linux\/#Sending_Parameters_to_a_Function\" >Sending Parameters to a Function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-here-documents-in-bash-on-linux\/#Creating_and_Sending_an_Email\" >Creating and Sending an Email<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-here-documents-in-bash-on-linux\/#Using_Here_Documents_with_SSH\" >Using Here Documents with SSH<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-here-documents-in-bash-on-linux\/#Strange_Name_Neat_Features\" >Strange Name, Neat Features<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Use \u201cHere Documents\u201d in Bash on Linux&#8221;<\/strong><\/p>\n<div>\n<figure style=\"width: 650px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-442612 size-full\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/09\/stock-lede-linux-see-attribution.png?width=1200&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"A terminal window on a Linux computer system.\" width=\"650\" height=\"300\" data-credittext=\"Fatmawati Achmad Zaenuri\/Shutterstock\" data-crediturl=\"https:\/\/www.shutterstock.com\/image-vector\/linux-interface-screen-notebook-world-map-321627716\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-vector\/linux-interface-screen-notebook-world-map-321627716\">Fatmawati Achmad Zaenuri\/Shutterstock<\/a><\/span><\/figcaption><\/figure>\n<p>The strangely named \u201chere documents\u201d let you use input\/out redirection inside Bash <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">script<\/a>s on Linux. They\u2019re a great way to automate commands you need to run on a remote computer.<\/p>\n<h2 id=\"here-documents\"><span class=\"ez-toc-section\" id=\"Here_Documents\"><\/span>Here Documents<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Many commands in Linux have two or three letter names. This is partly what gives rise to the notion that Linux is hard to learn and full of arcane commands. But one of the weirdest names in Linux isn\u2019t one of the cryptically short ones. \u201cHere documents\u201d aren\u2019t documents, and it isn\u2019t really clear what the \u201chere\u201d refers to, either.<\/p>\n<p>They are a relatively obscure construct, but they are useful. Of course, this is Linux, so there\u2019s more than one way to skin a cat. Some of the functionality provided by here documents can be reproduced in other ways. Those alternate methods are usually more complicated. In programming and scripting, \u201cmore complicated\u201d also means \u201cmore prone to bugs,\u201d and that your code is harder to maintain.<\/p>\n<p>Where here documents really excel is in the automation of commands that you want to send to a remote computer from a connection established from within a script. Making the connection is easy, but once the connection has been made, how do you \u201cpump\u201d your commands from your script into the shell on the remote computer? Here documents let you do that very simply.<\/p>\n<h2 id=\"basic-principles-of-here-documents\"><span class=\"ez-toc-section\" id=\"Basic_Principles_of_Here_Documents\"><\/span>Basic Principles of Here Documents<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The idiomatic representation of a here document looks like this:<\/p>\n<pre>COMMAND &lt;&lt; limit_string&#13;\n .&#13;\n .&#13;\ntext &#13;\ndata&#13;\nvariables&#13;\n.&#13;\n.&#13;\nlimit_string<\/pre>\n<ul>\n<li><strong>COMMAND<\/strong>: This can be any Linux command that accepts redirected input. Note, the <code>echo<\/code> command <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/man7.org\/linux\/man-pages\/man1\/echo.1.html\">doesn\u2019t accept redirected input<\/a>. If you need to write to the screen, you can use the <code>cat<\/code> command, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/man7.org\/linux\/man-pages\/man1\/cat.1.html\">which does<\/a>.<\/li>\n<li><strong>&lt;&lt;<\/strong>: The redirection operator.<\/li>\n<li><strong>limit_string<\/strong>: This is a label. It can be whatever you like as long as it doesn\u2019t appear in the list of data you\u2019re redirecting into the command. It is used to mark the end of the text, data, and variables list.<\/li>\n<li><strong>Data List<\/strong>: A list of data to be fed to the command. It can contain commands, text, and variables. The contents of the data list are fed into the command one line at a time until the _limit_string is encountered.<\/li>\n<\/ul>\n<p>You\u2019ll probably see examples of here documents that use \u201cEOF\u201d as the limit string. We don\u2019t favor that approach. It works, but \u201cEOF\u201d means \u201cEnd of File.\u201d Apart from the rare case where a home document is the last thing in a script file, \u201cEOF\u201d is being used erroneously.<\/p>\n<p>It will make your scripts much more readable if you use a limit string that refers to what you\u2019re doing. If you\u2019re sending a <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/watch-movies-tv-seriess\/\" data-internallinksmanager029f6b8e52c=\"8\" title=\"Watch Movies &amp; TV Series\" target=\"_blank\" rel=\"noopener\">series<\/a> of commands to a remote computer over <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/man7.org\/linux\/man-pages\/man1\/ssh.1.html\">Secure Shell<\/a> (SSH), a limit string called something like \u201c_remote_commands\u201d would make perfect sense. You don\u2019t need to start them with an underscore \u201c<code>_<\/code>\u201d character. We do that because it marks them as something out of the ordinary in your script.<\/p>\n<h2 id=\"simple-examples\"><span class=\"ez-toc-section\" id=\"Simple_Examples\"><\/span>Simple Examples<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can use here documents on the command line and in scripts. When you type the following in a terminal window, you\u2019ll see a \u201c<code>&gt;<\/code>\u201d line continuation prompt each time you hit \u201cEnter.\u201d When you type the \u201c_end_of_text\u201d limit string and hit \u201cEnter,\u201d the list of websites is passed to <code>cat,<\/code> and they are displayed in the terminal window.<\/p>\n<pre>cat &lt;&lt; _end_of_text &#13;\nHow-To Geek &#13;\nReview Geek &#13;\nLifeSavvy &#13;\nCloudSavvy\u00a0IT&#13;\nMindBounce&#13;\n_end_of_text<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-719084\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/03\/1-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"167\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>That\u2019s not the most worthwhile of exercises, but it does demonstrate that nothing is sent to the command until the entire list of data is collated and the limit string is encountered.\u00a0The <code>cat<\/code> command doesn\u2019t receive any input until you enter the limit string \u201c_end_of_text\u201d and hit the \u201cEnter\u201d key.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-719085\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/03\/2-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"282\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We can do the same thing in a script. Type or copy this example into an editor, save the file as \u201cheredoc-1.sh\u201d, and close the editor.<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\ncat &lt;&lt; \"_end_of_text\"&#13;\nYour user name is: $(whoami)&#13;\nYour current working directory is: $PWD&#13;\nYour Bash version is: $BASH_VERSION&#13;\n_end_of_text<\/pre>\n<p>As you follow this article, each time you create a script, you\u2019ll need to <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/man7.org\/linux\/man-pages\/man1\/chmod.1.html\">make it executable<\/a> before it will run. In each case, use the <code>chmod<\/code> command. Substitute the name of the script in each example for the script name used here.<\/p>\n<pre>chmod +x heredoc-1.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-719086\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/03\/3-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"57\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>This script contains two environment variables, <code>$PWD<\/code> and <code>$BASH_VERSION<\/code>. The environment variable names are replaced by their data values\u2014the current working directory and the version of Bash\u2014when the script is executed.<\/p>\n<p>The script also uses\u00a0<em>command substitution<\/em>\u00a0on the <code>whoami<\/code> command. The name of the command is replaced by its own output. The output from the entire script is written to the terminal window by the cat command. We run the script by calling it by name:<\/p>\n<pre>.\/heredoc-1.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-719088\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/03\/4-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"132\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If you modify the script and wrap the limit string in the first line of the here document in quotation marks \u201d <code>\"<\/code> \u201c, the data list is passed to the here document command verbatim. Variable names are displayed instead of variable values, and command substitution will not take place.<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\ncat &lt;&lt;- \"_end_of_text\"&#13;\nYour user name is: $(whoami)&#13;\nYour current working directory is: $PWD&#13;\nYour Bash version is: $BASH_VERSION&#13;\n_end_of_text<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-719089\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/03\/9-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"132\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 id=\"handling-tab-characters\"><span class=\"ez-toc-section\" id=\"Handling_Tab_Characters\"><\/span>Handling Tab Characters<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>By default, tab characters in your data list will be retained and written to the terminal window. Copy and save this example as \u201cheredoc-2.sh.\u201d Make it executable using the <code>chmod<\/code> command. Edit the indented lines to make sure that they have one or two tab characters at the start of the line rather than a series of spaces.<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\ncat &lt;&lt; _end_of_text&#13;\nYour user name is: $(whoami)&#13;\n  Your current working directory is: $PWD&#13;\n    Your Bash version is: $BASH_VERSION&#13;\n_end_of_text<\/pre>\n<pre>.\/heredoc-2.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-719094\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/03\/5-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"132\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The tabs are written to the terminal window.<\/p>\n<p>By adding a dash \u201c<code>-<\/code>\u201d to the redirection operator, the here document will ignore leading tab characters. Save this example as \u201cheredoc-3.sh\u201d and make it executable.<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\ncat &lt;&lt;- _end_of_text&#13;\nYour user name is: $(whoami)&#13;\n  Your current working directory is: $PWD&#13;\n    Your Bash version is: $BASH_VERSION&#13;\n_end_of_text<\/pre>\n<pre>.\/heredoc-3.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-719099\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/03\/15-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"132\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The tabs are ignored. This might seem trivial, but it is a neat way to cope with leading tabs due to indented sections of scripts.<\/p>\n<p>Loops and other logical constructs are usually indented. If your here document is contained in an indented section of a script, using a dash \u201c<code>-<\/code>\u201d with the redirection operator removes formatting issues caused by the leading tab characters.<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\nif true; then&#13;\n  cat &lt;&lt;- _limit_string&#13;\n  Line 1 with a leading tab.&#13;\n  Line 2 with a leading tab.&#13;\n  Line 3 with a leading tab.&#13;\n  _limit_string&#13;\nfi<\/pre>\n<h2 id=\"redirecting-to-a-file\"><span class=\"ez-toc-section\" id=\"Redirecting_to_a_File\"><\/span>Redirecting to a File<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The output from the command used with the here document can be redirected into a file. Use the \u201c<code>&gt;<\/code>\u201d (create the file) or \u201c<code>&gt;&gt;<\/code>\u201d (create the file if it doesn\u2019t exist, append to the file if it does) redirection operators\u00a0<em>after<\/em>\u00a0the limit string in the first line of the here document.<\/p>\n<p>This script is \u201cheredoc-4.sh.\u201d It will redirect its output to a text file called \u201csession.txt.\u201d<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\ncat &lt;&lt; _end_of_text &gt; session.txt&#13;\nYour user name is: $(whoami)&#13;\nYour current working directory is: $PWD&#13;\nYour Bash version is: $BASH_VERSION&#13;\n_end_of_text<\/pre>\n<pre>.\/heredoc-4.sh<\/pre>\n<pre>cat session.text<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-719100\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/03\/6-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"147\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 id=\"piping-the-output-to-another-command\"><span class=\"ez-toc-section\" id=\"Piping_the_Output_to_Another_Command\"><\/span>Piping the Output to Another Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The output from the command used in a here document can be piped as the input to another command. Use the pipe \u201c<code>|<\/code>\u201d operator\u00a0<em>after<\/em>\u00a0the limit string in the first line of the here document. We\u2019re going to pipe the output from the here document command,\u00a0<code>cat<\/code>, into\u00a0<code>sed<\/code>. We want to\u00a0<a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/man7.org\/linux\/man-pages\/man1\/sed.1.html\">substitute all occurrences<\/a> of the letter \u201ca\u201d with the letter \u201ce\u201d.<\/p>\n<p>Name this script \u201cheredoc-5.sh.\u201d<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\ncat &lt;&lt; _end_of_text | sed 's\/a\/e\/g'&#13;\nHow&#13;\nTo&#13;\nGaak&#13;\n_end_of_text<\/pre>\n<pre>.\/heredoc-5.sh<\/pre>\n<p>\u201cGaak\u201d is corrected to \u201cGeek.\u201d<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-719102\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/03\/7-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"132\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 id=\"sending-parameters-to-a-function\"><span class=\"ez-toc-section\" id=\"Sending_Parameters_to_a_Function\"><\/span>Sending Parameters to a Function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The command that is used with a here document can be a function in the script.<\/p>\n<p>This script passes some vehicle data into a function. The function reads the data as though it had been typed in by a user. The values of the variables are then printed. Save this script as \u201cheredoc-6.sh\u201d.<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\n# the set_car_details() function&#13;\nset_car_details ()&#13;\n{&#13;\nread make&#13;\nread model&#13;\nread new_used&#13;\nread delivery_collect&#13;\nread location&#13;\nread price&#13;\n}&#13;\n&#13;\n# The here document that passes the data to set_car_details()&#13;\nset_car_details &lt;&lt; _mars_rover_data&#13;\nNASA&#13;\nPerseverance Rover&#13;\nUsed&#13;\nCollect&#13;\nMars (long,lat) 77.451865,18.445161&#13;\n2.2 billion&#13;\n_mars_rover_data&#13;\n&#13;\n# Retrieve the vehicle details&#13;\necho \"Make: $make\"&#13;\necho \"Model: $model\"&#13;\necho \"New or Used: $new_used\"&#13;\necho \"Delivery or Collection: $delivery_collect\"&#13;\necho \"Location: $location\"&#13;\necho \"Price $: $price\"<\/pre>\n<pre>.\/heredoc-6.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-719103\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/03\/8-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"197\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The vehicle details are written to the terminal window.<\/p>\n<h2 id=\"creating-and-sending-an-email\"><span class=\"ez-toc-section\" id=\"Creating_and_Sending_an_Email\"><\/span>Creating and Sending an Email<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We can use a here document to compose and send an email. Note that we can pass parameters to the command in front of the redirection operator. We\u2019re<a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.cloudsavvyit.com\/719\/how-to-set-up-a-mail-agent-for-command-line-email\/\"> using the Linux <code>mail<\/code> command<\/a> to <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/linux.die.net\/man\/1\/mail\">send an email through the local mail system<\/a>\u00a0to the user account called \u201cdave\u201d. The <code>-s<\/code> (subject) option allows us to specify the subject for the email.<\/p>\n<p>This example forms script \u201cheredoc-7.sh.\u201d<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\narticle=\"Here Documents\"&#13;\n&#13;\nmail -s 'Workload status' dave &lt;&lt; _project_report&#13;\nUser name: $(whoami)&#13;\nHas completed assignment:&#13;\nArticle: $article&#13;\n_project_report<\/pre>\n<pre>.\/heredoc-7.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-719104\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/03\/10-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"57\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>There is no visible output from this script. But when we check our mail, we see that the email was composed, dispatched, and delivered.<\/p>\n<pre>mail<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-719105\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/03\/11-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"372\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 id=\"using-here-documents-with-ssh\"><span class=\"ez-toc-section\" id=\"Using_Here_Documents_with_SSH\"><\/span>Using Here Documents with SSH<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Here documents are a powerful and convenient way to execute some commands on a remote computer once an SSH connection has been established. If you\u2019ve set up SSH keys between the two computers, the login process will be fully automatic. In this quick and dirty example, you\u2019ll be prompted for the password for the user account on the remote computer.<\/p>\n<p>This script is \u201cheredoc-8.sh\u201d. We\u2019re going to connect to a remote computer called \u201cremote-pc\u201d. The user account is called \u201cdave\u201d. We\u2019re using the <code>-T<\/code> (disable pseudo-terminal allocation) option because we don\u2019t need an interactive pseudo-terminal to be assigned to us.<\/p>\n<p>In the \u201cdo some work in here\u201d section of the script, we could pass a list of commands, and these would be executed on the remote computer. Of course, you could just call a script that was on the remote computer. The remote script could hold all of the commands and routines that you want to have executed.<\/p>\n<p>All that our script\u2014heredoc-8.sh\u2014is going to do is update a connection log on the remote computer. The user account and a time and date stamp are logged to a text file.<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\nssh -T dave@remote-pc.local &lt;&lt; _remote_commands&#13;\n&#13;\n# do some work in here&#13;\n&#13;\n# update connection log&#13;\necho $USER \"-\" $(date) &gt;&gt; \/home\/dave\/conn_log\/script.log&#13;\n_remote_commands<\/pre>\n<p>When we run the command, we are prompted for the password for the account on the <em>remote<\/em> computer.<\/p>\n<pre>.\/heredoc-8.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-719107\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/03\/12-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"77\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Some information about the remote computer is displayed, and we\u2019re returned to the command prompt.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-719108\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/03\/13-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"307\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>On the <em>remote<\/em> computer, we can use <code>cat<\/code> to check the connection log:<\/p>\n<pre>cat conn_log\/script.log<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-719110\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/03\/14-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"646\" height=\"167\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Each connection is listed for us.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>How to Create and Install SSH Keys From the Linux Shell<\/em><\/strong><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Strange_Name_Neat_Features\"><\/span>Strange Name, Neat Features<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Here documents are quirky but powerful, especially when used to send commands to a remote computer. It\u2019d be a simple matter to script a backup routine using <code>rsync<\/code>. The script could then connect to the remote computer, check the remaining storage space, and send an alerting email if space was getting low.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>How to Back Up Your Linux System<\/em><\/strong><\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.howtogeek.com\/719058\/how-to-use-here-documents-in-bash-on-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Use \u201cHere Documents\u201d in Bash on Linux&#8221; Fatmawati Achmad Zaenuri\/Shutterstock The strangely named \u201chere documents\u201d let you use input\/out redirection inside Bash scripts on Linux. They\u2019re a great way to automate commands you need to run on a remote computer. Here Documents Many commands in Linux have two or three letter names. This&#8230;<\/p>\n","protected":false},"author":1,"featured_media":216219,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/09\/stock-lede-linux-see-attribution.png?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-216218","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/216218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=216218"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/216218\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/216219"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=216218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=216218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=216218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}