{"id":17090,"date":"2023-08-21T07:15:48","date_gmt":"2023-08-21T07:15:48","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=17090"},"modified":"2023-12-31T07:40:42","modified_gmt":"2023-12-31T07:40:42","slug":"bash-heredoc-variable","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/","title":{"rendered":"What is &#8220;HereDoc&#8221; Variable in Bash? [5 Practical Cases]"},"content":{"rendered":"<p style=\"text-align: justify;\">Being a Linux user, while working with complex or lengthy content, it\u2019s so usual to face a situation like when you need to redirect a block of text or code stream to an interactive command. In such a case, the <strong>HereDoc variable<\/strong> serves as the best option by embedding the multiline strings directly within the Bash scripts. However, let\u2019s explore this article to have a concise look at different aspects of HereDoc variables in Bash.<\/p>\n<div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/Practice-Files_Bash-HereDoc-Variable.zip\" class=\"su-button su-button-style-default\" style=\"color:#000000;background-color:#E8F9FF;border-color:#bac8cc;border-radius:56px\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"color:#000000;padding:8px 26px;font-size:20px;line-height:30px;border-color:#effbff;border-radius:56px;text-shadow:0px 0px 0px #000000\"><i class=\"sui sui-download\" style=\"font-size:20px;color:#1AA1D6\"><\/i> Practice Files for HereDoc Variable in Bash<\/span><\/a><\/div>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" 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: #999;color:#999\" 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><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#What_is_a_%E2%80%9CHereDoc%E2%80%9D_in_Bash\" >What is a &#8220;HereDoc&#8221; in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#What_is_Bash_%E2%80%9CHereDoc%E2%80%9D_Variable\" >What is Bash &#8220;HereDoc&#8221; Variable?<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#5_Cases_for_Bash_%E2%80%9CHereDoc%E2%80%9D_Variable\" >5 Cases for Bash &#8220;HereDoc&#8221; Variable<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#1_Create_Multiline_Message_Using_%E2%80%9CHereDoc%E2%80%9D_Variable_in_Bash\" >1. Create Multiline Message Using &#8220;HereDoc&#8221; Variable in Bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#2_Redirect_Content_Using_%E2%80%9CHereDoc%E2%80%9D_Variable_in_Bash\" >2. Redirect Content Using &#8220;HereDoc&#8221; Variable in Bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#3_Bash_%E2%80%9CHereDoc%E2%80%9D_Variable_as_Input_for_%E2%80%9Cgrep%E2%80%9D_Command\" >3. Bash &#8220;HereDoc&#8221; Variable as Input for &#8220;grep&#8221; Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#4_Bash_%E2%80%9CHereDoc%E2%80%9D_Variable_for_Dynamic_HTML_Generation\" >4. Bash &#8220;HereDoc&#8221; Variable for Dynamic HTML Generation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#5_Bash_%E2%80%9CHereDoc%E2%80%9D_Variable_for_Creating_a_Custom_Script\" >5. Bash &#8220;HereDoc&#8221; Variable for Creating a Custom Script<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#People_Also_Ask\" >People Also Ask<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#What_is_the_syntax_of_the_HereDoc\" >What is the syntax of the HereDoc?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#How_to_redirect_HereDoc_content_to_a_file\" >How to redirect HereDoc content to a file?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#How_to_prevent_variable_expansion_within_a_HereDoc\" >How to prevent variable expansion within a HereDoc?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#What_to_do_if_I_want_to_maintain_indentation_within_the_HereDoc\" >What to do if I want to maintain indentation within the HereDoc?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#Can_I_use_a_HereDoc_variable_to_provide_input_to_a_command\" >Can I use a HereDoc variable to provide input to a command?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#How_can_I_remove_line_breaks_from_the_HereDoc_content\" >How can I remove line breaks from the HereDoc content?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#Can_I_use_command_substitution_within_a_HereDoc\" >Can I use command substitution within a HereDoc?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#What_is_cat\" >What is cat &lt;&lt;EOF in bash?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"What_is_a_%E2%80%9CHereDoc%E2%80%9D_in_Bash\"><\/span>What is a &#8220;HereDoc&#8221; in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">HereDoc <strong>(Here Document) <\/strong>is a feature that allows you to define multiple lines of text within your code in Bash. Generally, it starts with a delimiter and ends with the same delimiter at the beginning of a line. And inside the delimiter, you can include any kind of plain text or variable reference as HereDoc content.<\/p>\n<p>Here&#8217;s a basic syntax of HereDoc in Bash:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">COMMAND &lt;&lt; DELIMITER\nHereDoc Text\nDELIMITER<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"What_is_Bash_%E2%80%9CHereDoc%E2%80%9D_Variable\"><\/span>What is Bash &#8220;HereDoc&#8221; Variable?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">A <strong>Bash HereDoc variable<\/strong> is a powerful tool that uses the HereDoc construct, captures multiple content blocks, and assigns it to a variable. This variable is useful not only for storing configuration files, complex strings, and variable references but also for containing command substitutions in case of making dynamic content.<\/p>\n<h2 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"5_Cases_for_Bash_%E2%80%9CHereDoc%E2%80%9D_Variable\"><\/span>5 Cases for Bash &#8220;HereDoc&#8221; Variable<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">There are various ways to use the <strong>HereDoc variable in Bash<\/strong>. The following section showcases <strong>5 cases<\/strong> of Bash HereDoc variable:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_Create_Multiline_Message_Using_%E2%80%9CHereDoc%E2%80%9D_Variable_in_Bash\"><\/span>1. Create Multiline Message Using &#8220;HereDoc&#8221; Variable in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Multiline messages can be created with the <strong>HereDoc<\/strong> variable in bash. <strong>Here document<\/strong> allows you to define a block of messages within a script or command. It can include multiline text without escaping any special characters.<\/p>\n<p style=\"text-align: justify;\">Now, to create multiline messages using <strong>HereDoc<\/strong> variables in Bash, follow the script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nname=&quot;X&quot;\n\nmulti_msg=$(cat &lt;&lt;EOF\nHello, $name!\nThis is an example\nIt is a multiline message with HereDoc variable\nEOF\n)\n\necho &quot;$multi_msg&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">In this script, <code class=\"\" data-line=\"\">name=&quot;X&quot;<\/code> assigns the value <strong>X <\/strong>to the variable <strong>name<\/strong>. Then, the line <code class=\"\" data-line=\"\">multi_msg=$(cat &lt;&lt;EOF \u2026 EOF)<\/code> specifies a HereDoc section. The text between the delimiter <code class=\"\" data-line=\"\">&lt;&lt;EOF<\/code> and <code class=\"\" data-line=\"\">EOF<\/code> is assigned to the variable <strong>multi_msg<\/strong>. Next, in the line <code class=\"\" data-line=\"\">echo &quot;$multi_msg&quot;<\/code>, the <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/input-output\/output\/echo-command\/\" target=\"_blank\" rel=\"noopener\"><strong>echo command<\/strong><\/a> prints the text of the <strong>multi_msg<\/strong>\u00a0variable.<\/p>\n<\/div><\/div>\n<p style=\"text-align: justify;\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-17115\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/img3-Created-multiline-message-using-bash-HereDoc-variable.png\" alt=\"Created multiline message using Bash HereDoc variable\" width=\"825\" height=\"234\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/img3-Created-multiline-message-using-bash-HereDoc-variable.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/img3-Created-multiline-message-using-bash-HereDoc-variable-300x85.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/img3-Created-multiline-message-using-bash-HereDoc-variable-765x217.png 765w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>From the image, you can see the <strong>multiline message<\/strong> that I have created using the <strong>HereDoc<\/strong> variable.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"2_Redirect_Content_Using_%E2%80%9CHereDoc%E2%80%9D_Variable_in_Bash\"><\/span>2. Redirect Content Using &#8220;HereDoc&#8221; Variable in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">You can use the <strong>HereDoc<\/strong> variable to define text and then redirect the content to a file. This lets you overwrite a file with the stored text of the HereDoc variable.<\/p>\n<p style=\"text-align: justify;\">Let&#8217;s see a bash script of how to redirect content using the <strong>HereDoc<\/strong> variable in bash:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\noutput_file=&quot;file.txt&quot;\n\ntext=$(cat &lt;&lt;EOF\nHello, there!\nThis symbolizes HereDoc variable redirection.\nEOF\n)\n\necho &quot;$text&quot; &gt; &quot;$output_file&quot;\necho &quot;Text has been redirected to $output_file&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">Here, the line <code class=\"\" data-line=\"\">output_file=&quot;file.txt&quot;<\/code> assigns a file <strong>file.txt<\/strong> to the variable <strong>output_file<\/strong>. Next, <code class=\"\" data-line=\"\">text=$(cat &lt;&lt;EOF \u2026 EOF)<\/code> specifies a HereDoc block. The text between the delimiter <code class=\"\" data-line=\"\">&lt;&lt;EOF<\/code> and <code class=\"\" data-line=\"\">EOF<\/code> is assigned to the variable <strong>text<\/strong>. Then, the line <code class=\"\" data-line=\"\">echo &quot;$text&quot; &gt; &quot;$output_file&quot;<\/code> redirects the content of the <strong>text<\/strong> variable to the file assigned to the <strong>output_file<\/strong> variable. Finally, in <code class=\"\" data-line=\"\">echo &quot;Text has been redirected to $output_file&quot;<\/code>, the <strong>echo <\/strong>command prints <strong>Text has been redirected to $output_file<\/strong>\u00a0to confirm the redirection of the HereDoc content.<\/p>\n<\/div><\/div>\n<p style=\"text-align: justify;\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-17116\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/img4-Redirection-Using-HereDoc-Variable.png\" alt=\"Redirection Using HereDoc Variable\" width=\"825\" height=\"187\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/img4-Redirection-Using-HereDoc-Variable.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/img4-Redirection-Using-HereDoc-Variable-300x68.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/img4-Redirection-Using-HereDoc-Variable-768x174.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>From the image, you can see that the content of the HereDoc variable has been redirected to the file <strong>file.txt<\/strong>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3_Bash_%E2%80%9CHereDoc%E2%80%9D_Variable_as_Input_for_%E2%80%9Cgrep%E2%80%9D_Command\"><\/span>3. Bash &#8220;HereDoc&#8221; Variable as Input for &#8220;grep&#8221; Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The following example demonstrates how you can use the HereDoc variable as input to the <a href=\"https:\/\/linuxsimply.com\/grep-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>grep command<\/strong><\/a> to search for specific patterns. Check the bash script to know how to use the HereDoc variable as <strong>input<\/strong> of the <code class=\"\" data-line=\"\">grep<\/code> command:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Defining a grep pattern\ngrep_pattern=&quot;linuxsimply&quot;\n\n#Defining &#039;content&#039; as a HereDoc variable\ncontent=$(cat &lt;&lt;EOF\nThis line contains &#039;linuxsimply&#039;.\nSearch for: $grep_pattern\nEOF\n)\n\n#Using &#039;grep&#039; for searching the matching lines with the defined pattern\necho &quot;$content&quot; | grep &quot;$grep_pattern&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>Here, <code class=\"\" data-line=\"\">grep_pattern=&quot;linuxsimply&quot;\u00a0<\/code>assigns the pattern <strong>linuxsimply<\/strong> to the HereDoc variable <strong>grep_pattern<\/strong>. Next, <code class=\"\" data-line=\"\">content=$(cat &lt;&lt;EOF \u2026 EOF)<\/code> specifies a HereDoc block. The text with the specified search pattern between the delimiter <code class=\"\" data-line=\"\">&lt;&lt;EOF<\/code> and <code class=\"\" data-line=\"\">EOF<\/code> is assigned to the variable <strong>content<\/strong>. Now, in the line <code class=\"\" data-line=\"\">echo &quot;$content&quot; | grep &quot;$grep_pattern&quot;<\/code>, the text inside the <strong>content <\/strong>variable is piped to the <code class=\"\" data-line=\"\">grep<\/code> command where the command searches for lines that match with the specified pattern in the variable <strong>grep_pattern<\/strong>.<\/p>\n<\/div><\/div>\n<p style=\"text-align: justify;\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-17117\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/img5-HereDoc-variable-as-input-to-a-_grep_-command.png\" alt=\"HereDoc variable as input to a _grep_ command\" width=\"700\" height=\"199\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/img5-HereDoc-variable-as-input-to-a-_grep_-command.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/img5-HereDoc-variable-as-input-to-a-_grep_-command-300x85.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/img5-HereDoc-variable-as-input-to-a-_grep_-command-767x218.png 767w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>The above image shows the matched pattern in the HereDoc variable.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"4_Bash_%E2%80%9CHereDoc%E2%80%9D_Variable_for_Dynamic_HTML_Generation\"><\/span>4. Bash &#8220;HereDoc&#8221; Variable for Dynamic HTML Generation<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The following example is a basic illustration of the HereDoc variable in case of generating a simple &amp; dynamic HTML page along with an HTML file including dynamic data. Let&#8217;s check the bash script to learn how to use the bash HereDoc variable for Dynamic HTML generation:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\ntitle=&quot;A Dynamic Website&quot;\ncontent=&quot;&lt;h1&gt;Welcome to LinuxSimply&lt;\/h1&gt;&quot;\n\nhtml_file=$(cat &lt;&lt;EOF\n&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;$title&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n$content\n&lt;\/body&gt;\n&lt;\/html&gt;\nEOF\n)\n\n#Redirecting the html content to a file\necho &quot;$html_file&quot; &gt; file.html\n\necho &quot;A dynamic html page &#039;file.html&#039; is created.&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>Here, <code class=\"\" data-line=\"\">title=&quot;A Dynamic Website&quot;<\/code> assigns the string <strong>A Dynamic Website<\/strong> to the variable <strong>title<\/strong> and <code class=\"\" data-line=\"\">content=&quot;&lt;h1&gt;Welcome to LinuxSimply&lt;\/h1&gt;&quot;<\/code> assigns the string <strong>&lt;h1&gt;Welcome to LinuxSimply&lt;\/h1&gt; <\/strong>to the variable <strong>content<\/strong>. Then, a HereDoc variable is defined in the line <code class=\"\" data-line=\"\">html_file=$(cat &lt;&lt;EOF<\/code> and the content including the variable&#8217;s <strong>title<\/strong> and <strong>content<\/strong> between the delimiter <code class=\"\" data-line=\"\">&lt;&lt;EOF<\/code> and <code class=\"\" data-line=\"\">EOF<\/code> are assigned to the variable <strong>html_file<\/strong>.<\/p>\n<p>Afterward, in the line <code class=\"\" data-line=\"\">echo &quot;$html_file&quot; &gt; file.html,<\/code> the content of the <strong>html_file <\/strong>variable is redirected to the file <strong>file.html<\/strong>. Finally, the <code class=\"\" data-line=\"\">echo<\/code> command displays <strong>A dynamic html page &#8216;file.html&#8217; is created.<\/strong>\u00a0to confirm the creation of the dynamic <strong>HTML<\/strong> page.<\/p>\n<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-17118\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/img6-A-dynamic-html-page-generation-using-HereDoc-variable.png\" alt=\"A dynamic html page generation using HereDoc variable\" width=\"825\" height=\"185\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/img6-A-dynamic-html-page-generation-using-HereDoc-variable.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/img6-A-dynamic-html-page-generation-using-HereDoc-variable-300x67.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/img6-A-dynamic-html-page-generation-using-HereDoc-variable-767x172.png 767w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>The above image shows the creation of a dynamic HTML page <strong>file.html<\/strong>\u00a0using the HereDoc variable.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"5_Bash_%E2%80%9CHereDoc%E2%80%9D_Variable_for_Creating_a_Custom_Script\"><\/span>5. Bash &#8220;HereDoc&#8221; Variable for Creating a Custom Script<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Here\u2019s an example of how you can use the HereDoc variable for defining the content of your own customized script, redirect it to a file, and make it executable. Check the script for creating a custom script using the HereDoc variable:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Defining a custom script\ncustom=$(cat &lt;&lt;&#039;EOF&#039;\necho &quot;This is a customized script.&quot;\necho &quot;It performs an effective task.&quot;\nEOF\n)\n\n#Redirecting the content of the &#039;custom&#039; variable to a file\necho &quot;$custom&quot; &gt; script.sh\n\n#Making the script executable\nchmod u+x script.sh\n\necho &quot;A custom script &#039;script.sh&#039; is created and made executable.&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">In the line <code class=\"\" data-line=\"\">custom=$(cat &lt;&lt;&#039;EOF&#039;<\/code>, a HereDoc variable is defined, and the content between the delimiter <code class=\"\" data-line=\"\">&lt;&lt;&#039;EOF&#039;<\/code> and <code class=\"\" data-line=\"\">EOF<\/code> are assigned to the variable <strong>custom<\/strong>. Here, <code class=\"\" data-line=\"\">&lt;&lt;&#039;EOF&#039;<\/code> and <code class=\"\" data-line=\"\">EOF<\/code> are used to prevent the variable expansion. Next, the content of the <strong>custom<\/strong> variable is redirected to a custom script <strong>script.sh<\/strong>.<\/p>\n<p style=\"text-align: justify;\">After that, in the line <code class=\"\" data-line=\"\">chmod u+x script.sh,<\/code> the <a href=\"https:\/\/linuxsimply.com\/chmod-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>chmod command<\/strong><\/a> makes the script executable allowing permission to the owner of the file. Finally, the <code class=\"\" data-line=\"\">echo<\/code> command displays <strong>A custom script &#8216;script.sh&#8217; is created and made executable.<\/strong>\u00a0indicating that the custom script is successfully created and made executable.<\/p>\n<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-17119\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/img7-Created-a-custom-script-and-made-it-executable.png\" alt=\"Created a custom script and made it executable\" width=\"825\" height=\"167\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/img7-Created-a-custom-script-and-made-it-executable.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/img7-Created-a-custom-script-and-made-it-executable-300x61.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/img7-Created-a-custom-script-and-made-it-executable-766x155.png 766w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>The above snapshot depicts that I have created a custom script named <strong>script.sh <\/strong>and made it executable.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">So far, from the whole article you have got that the <strong>HereDoc variable <\/strong>is such a flexible tool in Bash that enables you to create dynamic files, manipulate multiline contents, etc. By leveraging this variable properly, you can handle complex text-based tasks in your scripts in a powerful and effective way.<\/p>\n<h2 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"People_Also_Ask\"><\/span>People Also Ask<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_syntax_of_the_HereDoc\"><\/span>What is the syntax of the HereDoc?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The syntax of HereDoc (here document) is:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">COMMAND &lt;&lt; DELIMITER\nHereDoc Text\nDELIMITER<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"How_to_redirect_HereDoc_content_to_a_file\"><\/span>How to redirect HereDoc content to a file?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To redirect HereDoc content to a file, you can use the following syntax:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">cat &lt;&lt;EOF &gt; file.txt\nhereDoc content.\nEOF<\/code><\/pre>\n<p style=\"text-align: justify;\">The content of HereDoc will be redirected to the <strong>file.txt<\/strong> file.<\/p>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"How_to_prevent_variable_expansion_within_a_HereDoc\"><\/span>How to prevent variable expansion within a HereDoc?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">You can prevent variable expansion within a HereDoc by enclosing the delimiter in single quotes (<strong>\u2018 \u2019<\/strong>).<\/p>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"What_to_do_if_I_want_to_maintain_indentation_within_the_HereDoc\"><\/span>What to do if I want to maintain indentation within the HereDoc?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">If you want to maintain indentation within the HereDoc, then you need to ensure the indentation of the heredoc delimiter along with the content.<\/p>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Can_I_use_a_HereDoc_variable_to_provide_input_to_a_command\"><\/span>Can I use a HereDoc variable to provide input to a command?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><strong>Yes<\/strong>, you can use a HereDoc variable when you want to append multiple lines of input to a command.<\/p>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"How_can_I_remove_line_breaks_from_the_HereDoc_content\"><\/span>How can I remove line breaks from the HereDoc content?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Append the <a href=\"https:\/\/linuxsimply.com\/tr-command-in-linux\/\"><strong>tr<\/strong><\/a> command to remove line breaks from the HereDoc content before assigning it to a variable.<\/p>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Can_I_use_command_substitution_within_a_HereDoc\"><\/span>Can I use command substitution within a HereDoc?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><strong>Yes<\/strong>, you can use command substitution within a HereDoc.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_cat\"><\/span>What is cat &lt;&lt;EOF in bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <code class=\"\" data-line=\"\">cat &lt;&lt;EOF<\/code> construct is known as a <strong>here document<\/strong> in bash which is used to contain a block of text within a script or command. The EOF delimiter stands for the <strong>end of file<\/strong> that indicates the beginning and end of a here document.<\/p>\n<p><script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [{\n    \"@type\": \"Question\",\n    \"name\": \"How to prevent variable expansion within a HereDoc?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"You can prevent variable expansion within a HereDoc by enclosing the delimiter in single quotes (\u2018 \u2019).\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"What to do if I want to maintain indentation within the HereDoc?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"If you want to maintain indentation within the HereDoc, then you need to ensure the indentation of the heredoc delimiter along with the content.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"Can I use a HereDoc variable to provide input to a command?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Yes, you can use a HereDoc variable when you want to append multiple lines of input to a command.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How can I remove line breaks from the HereDoc content?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Append the tr command to remove line breaks from the HereDoc content before assigning it to a variable.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"Can I use command substitution within a HereDoc?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Yes, you can use command substitution within a HereDoc.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"What is cat <<EOF in bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"The cat <<EOF construct is known as a here document in bash which is used to contain a block of text within a script or command. The EOF delimiter stands for the end of file that indicates the beginning and end of a here document.\"\n    }\n  }]\n}\n<\/script><br \/>\n<span style=\"font-size: 18pt; color: #003366;\"><strong>Related Articles<\/strong><\/span><\/p>\n<ul>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/built-in-variables\/\" target=\"_blank\" rel=\"noopener\">What Are Built-in Variables in Bash [2 Cases With Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/environment-variables\/\" target=\"_blank\" rel=\"noopener\">An Ultimate Guide of Using Bash Environment Variables<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/bashrc-environment-variable\/\" target=\"_blank\" rel=\"noopener\">The \u201c.bashrc\u201d Environment Variables [4 Cases]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/string-variable\/\" target=\"_blank\" rel=\"noopener\">String Variables in Bash [Manipulation, Interpolation &amp; Testing]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/array-variable\/\" target=\"_blank\" rel=\"noopener\">What is Variable Array in Bash? [4 Cases]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/special-variables\/\" target=\"_blank\" rel=\"noopener\">An Extensive Exploration of Bash Special Variables [9 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/boolean-variables\/\" target=\"_blank\" rel=\"noopener\">What is Boolean Variable in Bash? [3 Cases With Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/ps1\/\" target=\"_blank\" rel=\"noopener\">What is PS1 Variable in Bash? [3 Customization Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/ps3\/\" target=\"_blank\" rel=\"noopener\">What is PS3 Variable in Bash? [3 Practical Examples]<\/a><\/strong><\/li>\n<\/ul>\n<hr \/>\n<p><strong>&lt;&lt; Go Back to <span data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Types of Variables in Bash&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1065857,&quot;3&quot;:{&quot;1&quot;:0},&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/\" target=\"_blank\" rel=\"noopener\">Types of Variables in Bash<\/a><\/span> | <span data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Bash Variable&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:16768,&quot;10&quot;:1,&quot;11&quot;:4,&quot;17&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/\" target=\"_blank\" rel=\"noopener\">Bash Variables<\/a><\/span> | <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\" target=\"_blank\" rel=\"noopener\">Bash Scripting Tutorial<\/a><\/strong><\/p>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-center kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;center&quot;,&quot;id&quot;:&quot;17090&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\\\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;What is \\u0026quot;HereDoc\\u0026quot; Variable in Bash? [5 Practical Cases]&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\">\n            <span class=\"kksr-muted\">Rate this post<\/span>\n    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Being a Linux user, while working with complex or lengthy content, it\u2019s so usual to face a situation like when &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"What is &#8220;HereDoc&#8221; Variable in Bash? [5 Practical Cases]\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#more-17090\" aria-label=\"Read more about What is &#8220;HereDoc&#8221; Variable in Bash? [5 Practical Cases]\">Read more<\/a><\/p>\n","protected":false},"author":314905,"featured_media":17120,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[120],"class_list":["post-17090","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-scripting","tag-variable-types","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>What is &quot;HereDoc&quot; Variable in Bash? [5 Practical Cases]<\/title>\n<meta name=\"description\" content=\"Use Bash HereDoc variable as a versatile solution for handling complex multiline contents and managing the Bash scripts effectively.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is &quot;HereDoc&quot; Variable in Bash? [5 Practical Cases]\" \/>\n<meta property=\"og:description\" content=\"Use Bash HereDoc variable as a versatile solution for handling complex multiline contents and managing the Bash scripts effectively.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/\" \/>\n<meta property=\"og:site_name\" content=\"LinuxSimply\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/nadiba.rahman?mibextid=ZbWKwL\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-21T07:15:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-31T07:40:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/Feature-image-Bash-heredoc-variable.png\" \/>\n\t<meta property=\"og:image:width\" content=\"400\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Nadiba Rahman\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nadiba Rahman\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/types\\\/heredoc-variable\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/types\\\/heredoc-variable\\\/\"},\"author\":{\"name\":\"Nadiba Rahman\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/69b717a03a5d9dc48e683aaffe22d33f\"},\"headline\":\"What is &#8220;HereDoc&#8221; Variable in Bash? [5 Practical Cases]\",\"datePublished\":\"2023-08-21T07:15:48+00:00\",\"dateModified\":\"2023-12-31T07:40:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/types\\\/heredoc-variable\\\/\"},\"wordCount\":1441,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/types\\\/heredoc-variable\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/08\\\/Feature-image-Bash-heredoc-variable.png\",\"keywords\":[\"variable types\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/types\\\/heredoc-variable\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/types\\\/heredoc-variable\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/types\\\/heredoc-variable\\\/\",\"name\":\"What is \\\"HereDoc\\\" Variable in Bash? [5 Practical Cases]\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/types\\\/heredoc-variable\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/types\\\/heredoc-variable\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/08\\\/Feature-image-Bash-heredoc-variable.png\",\"datePublished\":\"2023-08-21T07:15:48+00:00\",\"dateModified\":\"2023-12-31T07:40:42+00:00\",\"description\":\"Use Bash HereDoc variable as a versatile solution for handling complex multiline contents and managing the Bash scripts effectively.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/types\\\/heredoc-variable\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/types\\\/heredoc-variable\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/types\\\/heredoc-variable\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/08\\\/Feature-image-Bash-heredoc-variable.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/08\\\/Feature-image-Bash-heredoc-variable.png\",\"width\":400,\"height\":400,\"caption\":\"Feature image-Bash HereDoc variable\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/types\\\/heredoc-variable\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Bash Scripting Tutorial\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Bash Variables\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Types of Variables in Bash\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/types\\\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"What is &#8220;HereDoc&#8221; Variable in Bash? [5 Practical Cases]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"name\":\"LinuxSimply\",\"description\":\"All About Linux\",\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/linuxsimply.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\",\"name\":\"LinuxSimply\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"width\":355,\"height\":48,\"caption\":\"LinuxSimply\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/69b717a03a5d9dc48e683aaffe22d33f\",\"name\":\"Nadiba Rahman\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Nadiba-Rahman-II-96x96.png\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Nadiba-Rahman-II-96x96.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Nadiba-Rahman-II-96x96.png\",\"caption\":\"Nadiba Rahman\"},\"description\":\"Hello, This is Nadiba Rahman, currently working as a Linux Content Developer Executive at SOFTEKO. I have completed my graduation with a bachelor\u2019s degree in Electronics &amp; Telecommunication Engineering from Rajshahi University of Engineering &amp; Technology (RUET).I am quite passionate about crafting. I really adore exploring and learning new things which always helps me to think transparently. And this curiosity led me to pursue knowledge about Linux. My goal is to portray Linux-based practical problems and share them with you. Read Full Bio\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/nadiba.rahman?mibextid=ZbWKwL\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/nadiba-rahman-443459171\"],\"url\":\"https:\\\/\\\/linuxsimply.com\\\/author\\\/nadiba\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is \"HereDoc\" Variable in Bash? [5 Practical Cases]","description":"Use Bash HereDoc variable as a versatile solution for handling complex multiline contents and managing the Bash scripts effectively.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/","og_locale":"en_US","og_type":"article","og_title":"What is \"HereDoc\" Variable in Bash? [5 Practical Cases]","og_description":"Use Bash HereDoc variable as a versatile solution for handling complex multiline contents and managing the Bash scripts effectively.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/nadiba.rahman?mibextid=ZbWKwL","article_published_time":"2023-08-21T07:15:48+00:00","article_modified_time":"2023-12-31T07:40:42+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/Feature-image-Bash-heredoc-variable.png","type":"image\/png"}],"author":"Nadiba Rahman","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Nadiba Rahman","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/"},"author":{"name":"Nadiba Rahman","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/69b717a03a5d9dc48e683aaffe22d33f"},"headline":"What is &#8220;HereDoc&#8221; Variable in Bash? [5 Practical Cases]","datePublished":"2023-08-21T07:15:48+00:00","dateModified":"2023-12-31T07:40:42+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/"},"wordCount":1441,"commentCount":0,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/Feature-image-Bash-heredoc-variable.png","keywords":["variable types"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/","name":"What is \"HereDoc\" Variable in Bash? [5 Practical Cases]","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/Feature-image-Bash-heredoc-variable.png","datePublished":"2023-08-21T07:15:48+00:00","dateModified":"2023-12-31T07:40:42+00:00","description":"Use Bash HereDoc variable as a versatile solution for handling complex multiline contents and managing the Bash scripts effectively.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/Feature-image-Bash-heredoc-variable.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/Feature-image-Bash-heredoc-variable.png","width":400,"height":400,"caption":"Feature image-Bash HereDoc variable"},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/heredoc-variable\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/linuxsimply.com\/"},{"@type":"ListItem","position":2,"name":"Bash Scripting Tutorial","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/"},{"@type":"ListItem","position":3,"name":"Bash Variables","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/"},{"@type":"ListItem","position":4,"name":"Types of Variables in Bash","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/"},{"@type":"ListItem","position":5,"name":"What is &#8220;HereDoc&#8221; Variable in Bash? [5 Practical Cases]"}]},{"@type":"WebSite","@id":"https:\/\/linuxsimply.com\/#website","url":"https:\/\/linuxsimply.com\/","name":"LinuxSimply","description":"All About Linux","publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/linuxsimply.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/linuxsimply.com\/#organization","name":"LinuxSimply","url":"https:\/\/linuxsimply.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","width":355,"height":48,"caption":"LinuxSimply"},"image":{"@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/69b717a03a5d9dc48e683aaffe22d33f","name":"Nadiba Rahman","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Nadiba-Rahman-II-96x96.png","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Nadiba-Rahman-II-96x96.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Nadiba-Rahman-II-96x96.png","caption":"Nadiba Rahman"},"description":"Hello, This is Nadiba Rahman, currently working as a Linux Content Developer Executive at SOFTEKO. I have completed my graduation with a bachelor\u2019s degree in Electronics &amp; Telecommunication Engineering from Rajshahi University of Engineering &amp; Technology (RUET).I am quite passionate about crafting. I really adore exploring and learning new things which always helps me to think transparently. And this curiosity led me to pursue knowledge about Linux. My goal is to portray Linux-based practical problems and share them with you. Read Full Bio","sameAs":["https:\/\/www.facebook.com\/nadiba.rahman?mibextid=ZbWKwL","https:\/\/www.linkedin.com\/in\/nadiba-rahman-443459171"],"url":"https:\/\/linuxsimply.com\/author\/nadiba\/"}]}},"_links":{"self":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/17090","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/users\/314905"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/comments?post=17090"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/17090\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/17120"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=17090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=17090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=17090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}