{"id":23913,"date":"2023-10-04T11:28:18","date_gmt":"2023-10-04T11:28:18","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=23913"},"modified":"2024-01-23T05:38:05","modified_gmt":"2024-01-23T05:38:05","slug":"bash-unbound-variable","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/","title":{"rendered":"How Unbound Variable Error Occurs in Bash [Cases &#038; Solutions]"},"content":{"rendered":"<p style=\"text-align: justify;\">There may be a situation when you want to use a variable, but it\u2019s not declared, assigned or initialized. In this effort, Bash raises an error alert called an <strong>\u2018unbound variable error\u2019<\/strong> as it maintains a precise variable handling format. In this article, you\u2019ll get to know different aspects of <strong>unbound variable errors in Bash<\/strong> that can help you make reliable shell scripts. So, let\u2019s explore!<\/p>\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\/scope\/unbound-variable\/#Key_Takeaways\" >Key Takeaways<\/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\/scope\/unbound-variable\/#Free_Downloads\" >Free Downloads<\/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\/scope\/unbound-variable\/#3_Cases_of_How_Unbound_Variable_Error_Occurs_in_Bash\" >3 Cases of How Unbound Variable Error Occurs in Bash<\/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\/scope\/unbound-variable\/#Case_1_Accessing_an_Undefined_Global_Variable_in_Bash\" >Case 1: Accessing an Undefined Global 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\/scope\/unbound-variable\/#Case_2_Accessing_an_Undefined_Local_Variable_Within_a_Function_in_Bash\" >Case 2: Accessing an Undefined Local Variable Within a Function 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\/scope\/unbound-variable\/#Case_3_Accessing_a_Declared_but_Not_Uninitialized_Variable_in_Bash\" >Case 3: Accessing a Declared but Not Uninitialized Variable in Bash<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/#Best_Practices_to_Prevent_Unbound_Variable_Errors_in_Bash\" >Best Practices to Prevent Unbound Variable Errors in Bash<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/#A_Defining_and_Initializing_Variables\" >A. Defining and Initializing Variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/#B_Checking_Variable_Existence_Using_Conditionals\" >B. Checking Variable Existence Using Conditionals<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/#C_Assigning_Default_Values_in_Bash_Script\" >C. Assigning Default Values in Bash Script<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/#D_Enabling_the_%E2%80%9Cnounset%E2%80%9D_Option_Using_the_%E2%80%9Cset_-u%E2%80%9D_Command\" >D. Enabling the &#8220;nounset&#8221; Option Using the &#8220;set -u&#8221; Command<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/#People_Also_Ask\" >People Also Ask<\/a><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Key_Takeaways\"><\/span>Key Takeaways<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Learning about the unbound variable in Bash.<\/li>\n<li>Exploring how to avoid unbound variable error.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Free_Downloads\"><\/span>Free Downloads<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Practice-Files_Bash-Unbound-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> Download the Practice Files<\/span><\/a><\/div>\n<h2><span class=\"ez-toc-section\" id=\"3_Cases_of_How_Unbound_Variable_Error_Occurs_in_Bash\"><\/span>3 Cases of How Unbound Variable Error Occurs in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/introduction\/what-is-bash-scripting\/\" target=\"_blank\" rel=\"noopener\"><strong>Bash scripting<\/strong><\/a>, an Unbound variable error generally occurs not only for undefined variables but also for uninitialized variables. In the following section, you will see <strong>three <\/strong>different examples of how unbound variable error occurs for both global and local variables:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Case_1_Accessing_an_Undefined_Global_Variable_in_Bash\"><\/span>Case 1: Accessing an Undefined Global Variable in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">If you ever try to access an <strong>undefined global variable<\/strong>, you will encounter an unbound variable error. Here\u2019s such an example:<\/p>\n<p><span style=\"color: #800080;\"><strong>Steps to Follow &gt;<\/strong><\/span><\/p>\n<p style=\"text-align: justify;\">\u278a Go to <strong>Ubuntu <\/strong><a href=\"https:\/\/linuxsimply.com\/what-is-terminal-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>Terminal<\/strong><\/a>, open a script in the <strong>nano text editor<\/strong> by running the following command:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">nano global.sh<\/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<ul>\n<li><a href=\"https:\/\/linuxsimply.com\/nano-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>nano<\/strong><\/a>: A text editor.<\/li>\n<li style=\"text-align: justify;\"><strong>global.sh<\/strong>: This is a script. Here, I have named the script <strong>\u2018global.sh\u2019<\/strong>. You can name any of your choices.<\/li>\n<\/ul>\n<\/div><\/div>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-23919\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img1-Opening-the-file-in-Nano-text-editor.png\" alt=\"Opening the file in Nano text editor\" width=\"575\" height=\"166\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img1-Opening-the-file-in-Nano-text-editor.png 575w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img1-Opening-the-file-in-Nano-text-editor-300x87.png 300w\" sizes=\"(max-width: 575px) 100vw, 575px\" \/><\/p>\n<p>\u278b Now, <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/basics\/writing\/\" target=\"_blank\" rel=\"noopener\"><strong>write the following script<\/strong><\/a> inside the editor:<\/p>\n<p><span style=\"color: #800080;\"><strong>Script (global.sh) &gt;<\/strong><\/span><\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Trying to use an undefined global variable\necho &quot;The value of undefined_variable is: $undefined_variable&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, in <strong>#!\/bin\/bash<\/strong>, \u2018<strong>#!<\/strong>\u2019 is called <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/basics\/writing\/what-is-shebang\/\" target=\"_blank\" rel=\"noopener\">\u2018<strong>Shebang<\/strong>\u2019 or \u2018<strong>Hashbang<\/strong>\u2019<\/a>. This script indicates the unbound variable error caused while accessing the <strong>undefined global variable<\/strong>.<\/p>\n<\/div><\/div>\n<p><a id=\"Steps-to-Follow\"><\/a>\u278c Then, press <strong>CTRL+S<\/strong> to save the file &amp; press <strong>CTRL+X<\/strong> to exit.<\/p>\n<p>\u278d After that, use the command below to <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/basics\/executing\/make-file-executable\/\" target=\"_blank\" rel=\"noopener\"><strong>make the script executable<\/strong><\/a>:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">chmod u+x global.sh<\/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<ul>\n<li><a href=\"https:\/\/linuxsimply.com\/chmod-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>chmod<\/strong><\/a>: Changes the permission of the files and directories.<\/li>\n<li><strong>u+x<\/strong>: Adds the executable permission for the user.<\/li>\n<li><strong>global.sh<\/strong>: The file which you want to make executable.<\/li>\n<\/ul>\n<\/div><\/div>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-23920\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img2-Adding-executable-permission-to-the-script.png\" alt=\"Adding executable permission to the script\" width=\"622\" height=\"176\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img2-Adding-executable-permission-to-the-script.png 622w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img2-Adding-executable-permission-to-the-script-300x85.png 300w\" sizes=\"(max-width: 622px) 100vw, 622px\" \/><\/p>\n<p>\u278e Finally, <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/basics\/executing\/run-a-script\/\" target=\"_blank\" rel=\"noopener\"><strong>run the script<\/strong><\/a> by the following command:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">.\/global.sh<\/code><\/pre>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-23921\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img3-An-unbound-variable-error-occurs-due-to-undefined-global-variable.png\" alt=\"An 'unbound variable' error occurs due to undefined global variable\" width=\"700\" height=\"211\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img3-An-unbound-variable-error-occurs-due-to-undefined-global-variable.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img3-An-unbound-variable-error-occurs-due-to-undefined-global-variable-300x90.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p style=\"text-align: justify;\">From the image, you can see that an unbound variable error occurs because of accessing an undefined global variable <strong>\u2018undefined_variable\u2019<\/strong>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Case_2_Accessing_an_Undefined_Local_Variable_Within_a_Function_in_Bash\"><\/span>Case 2: Accessing an Undefined Local Variable Within a Function in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">When attempting to access a <strong>local variable inside a function<\/strong> that is not declared yet, it would result in an \u2018unbound variable\u2019 error for sure. You can see the following example as a demo.<\/p>\n<p style=\"text-align: justify;\"><div class=\"su-note\"  style=\"border-color:#e5e5e5;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#ffffff;border-color:#ffffff;color:#333333;\">You can follow the <a href=\"#Steps-to-Follow\"><strong>Steps of Case 1<\/strong><\/a>, to save &amp; make the script executable.<\/div><\/div>\n<p><span style=\"color: #800080;\"><strong>Script (local.sh) &gt;<\/strong><\/span><\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nfunction my_func() {\n#Trying to use an undefined local variable\necho &quot;The value of local_variable is: $local_variable&quot;\n}\nmy_func<\/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;\">The script finds an unbound variable error while accessing the <strong>undefined local variable<\/strong> inside a function.<\/p>\n<\/div><\/div>\n<p>Now, run the script by the following command:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">.\/local.sh<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-23922\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img4-An-unbound-variable-error-occurs-due-to-undefined-local-variable.png\" alt=\"An 'unbound variable' error occurs due to undefined local variable\" width=\"655\" height=\"164\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img4-An-unbound-variable-error-occurs-due-to-undefined-local-variable.png 655w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img4-An-unbound-variable-error-occurs-due-to-undefined-local-variable-300x75.png 300w\" sizes=\"(max-width: 655px) 100vw, 655px\" \/><\/p>\n<p style=\"text-align: justify;\">From the image, you can see that an unbound variable error occurs because of accessing an undefined local variable <strong>\u2018local_variable\u2019<\/strong>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Case_3_Accessing_a_Declared_but_Not_Uninitialized_Variable_in_Bash\"><\/span>Case 3: Accessing a Declared but Not Uninitialized Variable in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">It\u2019s not necessary that a variable must be undefined to create an <strong>unbound variable<\/strong> error. Accessing a variable that is already declared but not initialized may also cause this error. Here\u2019s an example of an unbound variable error due to an uninitialized variable.<\/p>\n<p style=\"text-align: justify;\"><div class=\"su-note\"  style=\"border-color:#e5e5e5;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#ffffff;border-color:#ffffff;color:#333333;\">You can follow the <a href=\"#Steps-to-Follow\"><strong>Steps of Case 1<\/strong><\/a>, to save &amp; make the script executable.<\/div><\/div>\n<p><span style=\"color: #800080;\"><strong>Script (uninitialized.sh) &gt;<\/strong><\/span><\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Defining a variable\nuninitialized=&quot;&quot;\n\n#Trying to use an uninitialized variable\necho &quot;The uninitialized_variable is: $uninitialized&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;\">The above script specifies an unbound variable error that occurred due to a declared but uninitialized variable.<\/p>\n<\/div><\/div>\n<p>Now, run the script by the following command:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">.\/uninitialized.sh<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-23923\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img5-An-unbound-variable-error-occurs-due-to-uninitialized-variable.png\" alt=\"An 'unbound variable' error occurs due to uninitialized variable\" width=\"700\" height=\"173\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img5-An-unbound-variable-error-occurs-due-to-uninitialized-variable.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img5-An-unbound-variable-error-occurs-due-to-uninitialized-variable-300x74.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p style=\"text-align: justify;\">In the above image, you can see an unbound variable error that occurs during the access of a declared but uninitialized variable <strong>\u2018uninitialized\u2019<\/strong>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Best_Practices_to_Prevent_Unbound_Variable_Errors_in_Bash\"><\/span>Best Practices to Prevent Unbound Variable Errors in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">If you want to minimize the risk of unbound variable errors in your script, you must define and initialize all the variables. Otherwise, it will cause an explicit error within the script. Also, you can either check a variable\u2019s existence before accessing it or set a default value to the variable or enable a \u2018nounset\u2019 option to handle these issues. Here, the following section illustrates some best practices you should follow to prevent unbound variable errors in Bash:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"A_Defining_and_Initializing_Variables\"><\/span>A. Defining and Initializing Variables<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Always declare and initialize variables before attempting to access and use them. In case of <strong>global variables<\/strong>, you can declare them at the start of your script and for <strong>local variables<\/strong>, you can use <strong>\u2018local\u2019 <\/strong>within functions to declare and initialize them. Let\u2019s have a look at the following instance.<\/p>\n<p style=\"text-align: justify;\"><div class=\"su-note\"  style=\"border-color:#e5e5e5;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#ffffff;border-color:#ffffff;color:#333333;\">You can follow the <a href=\"#Steps-to-Follow\"><strong>Steps of Case 1<\/strong><\/a>, to save &amp; make the script executable.<\/div><\/div>\n<p><span style=\"color: #800080;\"><strong>Script (define.sh) &gt;<\/strong><\/span><\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Defining and initializing a global variable\nglobal=&quot;A global variable&quot;\n\nfunction my_func() {\n#Defining and initializing a local variable\nlocal local_variable=&quot;A local variable&quot;\necho &quot;Variable inside the function: $local_variable&quot;\n}\nmy_func\n\necho &quot;Variable outside the function: $global&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;\">This script demonstrates a clear declaration and initialization of a global variable <strong>\u2018global\u2019 <\/strong>and a local variable <strong>\u2018local_variable\u2019<\/strong>. Finally, the script terminates by displaying the outputs using the <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/input-output\/output\/echo-command\/\" target=\"_blank\" rel=\"noopener\"><strong>echo commands<\/strong><\/a>.<\/p>\n<\/div><\/div>\n<p>Now, run the script by the following command:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">.\/define.sh<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-23924\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img6-Defining-and-initializing-global-and-local-variables-to-prevent-unbound-variable-error.png\" alt=\"Defining and initializing global and local variables to prevent unbound variable error\" width=\"825\" height=\"230\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img6-Defining-and-initializing-global-and-local-variables-to-prevent-unbound-variable-error.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img6-Defining-and-initializing-global-and-local-variables-to-prevent-unbound-variable-error-300x84.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img6-Defining-and-initializing-global-and-local-variables-to-prevent-unbound-variable-error-768x214.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">The above image depicts that if any global or local variables are defined or initialized before, then no unbound variable error can affect your scripts.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"B_Checking_Variable_Existence_Using_Conditionals\"><\/span>B. Checking Variable Existence Using Conditionals<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">You can use the <strong>\u2018if&#8230;else\u2019<\/strong> conditional statement to verify whether a variable exists and contains any values before accessing it. For instance:<\/p>\n<p style=\"text-align: justify;\"><div class=\"su-note\"  style=\"border-color:#e5e5e5;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#ffffff;border-color:#ffffff;color:#333333;\">You can follow the <a href=\"#Steps-to-Follow\"><strong>Steps of Case 1<\/strong><\/a>, to save &amp; make the script executable.<\/div><\/div>\n<p><span style=\"color: #800080;\"><strong>Script (existence.sh) &gt;<\/strong><\/span><\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Checking if the variable exists\nif [ -n &quot;$var&quot; ]; then\necho &quot;The variable exists: $var&quot;\nelse\necho &quot;The variable doesn\u2019t exist.&quot;\nfi<\/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;\">The above script checks if there is any existent variable called <strong>\u2018var\u2019<\/strong> by using the <strong>\u2018if..else\u2019<\/strong> conditional and displays the result using the echo command.<\/p>\n<\/div><\/div>\n<p>Now, run the script by the following command:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">.\/existence.sh<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-23925\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img7-Checking-if-variable-exists-using-if-conditional-to-prevent-unbound-variable-error.png\" alt=\"Checking if variable exists using 'if' conditional to prevent unbound variable error\" width=\"700\" height=\"188\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img7-Checking-if-variable-exists-using-if-conditional-to-prevent-unbound-variable-error.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img7-Checking-if-variable-exists-using-if-conditional-to-prevent-unbound-variable-error-300x81.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p style=\"text-align: justify;\">The above image indicates the prechecking of a variable\u2019s existence. First, ensure that the specific variable exists and then proceed to access it to avoid the unbound variable error in your scripts.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"C_Assigning_Default_Values_in_Bash_Script\"><\/span>C. Assigning Default Values in Bash Script<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Another way to handle the unbound variable errors in the script is to use default values for a variable. When a variable is not potentially defined or initialized or empty, you can use the syntax \u2018<strong>${variable:-default_value}<\/strong>\u2019 and assign any type of default value to the variable. Then, instead of showing errors, it provides the default fallback values you assigned to the variable. Let\u2019s see an example.<\/p>\n<p style=\"text-align: justify;\"><div class=\"su-note\"  style=\"border-color:#e5e5e5;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#ffffff;border-color:#ffffff;color:#333333;\">You can follow the <a href=\"#Steps-to-Follow\"><strong>Steps of Case 1<\/strong><\/a>, to save &amp; make the script executable.<\/div><\/div>\n<p><span style=\"color: #800080;\"><strong>Script (default.sh) &gt;<\/strong><\/span><\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Using default value to access variable\nvariable=&quot;${variable:-default_value}&quot;\n\necho &quot;The variable is: $variable&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 script assigns a default value to a variable to avoid unbound variable error and then echoes the output availing the echo command.<\/p>\n<\/div><\/div>\n<p>Now, run the script by the following command:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">.\/default.sh<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-23926\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img8-Using-default-value-to-access-a-variable-to-prevent-unbound-variable-error.png\" alt=\"Using default value to access a variable to prevent unbound variable error\" width=\"700\" height=\"202\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img8-Using-default-value-to-access-a-variable-to-prevent-unbound-variable-error.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img8-Using-default-value-to-access-a-variable-to-prevent-unbound-variable-error-300x87.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p style=\"text-align: justify;\">From the above image, you can see that by assigning a default value in an undefined or empty variable, you can easily handle the unbound variable error.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"D_Enabling_the_%E2%80%9Cnounset%E2%80%9D_Option_Using_the_%E2%80%9Cset_-u%E2%80%9D_Command\"><\/span>D. Enabling the &#8220;nounset&#8221; Option Using the &#8220;set -u&#8221; Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">There is one more option that is kind of optional but will help you point out unbound variable issues in your script. And that is the <strong>\u2018nounset\u2019 option<\/strong>. When you use the <strong>\u2018set -u\u2019<\/strong> command at the beginning of the script, Bash enables the \u2018nounset\u2019 option, terminates your script and treats the undefined variables as errors. This approach is definitely helpful for identifying and fixing unbound variable errors gracefully during script development.<\/p>\n<p style=\"text-align: justify;\"><div class=\"su-note\"  style=\"border-color:#e5e5e5;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#ffffff;border-color:#ffffff;color:#333333;\">You can follow the <a href=\"#Steps-to-Follow\"><strong>Steps of Case 1<\/strong><\/a>, to save &amp; make the script executable.<\/div><\/div>\n<p><span style=\"color: #800080;\"><strong>Script (optional.sh) &gt;<\/strong><\/span><\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Enabling &#039;nounset&#039; option to identify unbound variable errors\nset -u\n\necho &quot;The variable is: $variable&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 script uses the <strong>\u2018set -u\u2019<\/strong> command judiciously to enable the <strong>\u2018nounset\u2019 <\/strong>option, identify the unbound variable error and stop the execution. At last, it employs the echo command to display the error message.<\/p>\n<\/div><\/div>\n<p>Now, run the script by the following command:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">.\/optional.sh<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-23927\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img9-Enabling-the-nounset-set-u-option-at-the-beginning-of-the-script-to-prevent-unbound-variable-error.png\" alt=\"Enabling the 'nounset' option using the 'set -u' command at the beginning of the script to prevent unbound variable error\" width=\"700\" height=\"175\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img9-Enabling-the-nounset-set-u-option-at-the-beginning-of-the-script-to-prevent-unbound-variable-error.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img9-Enabling-the-nounset-set-u-option-at-the-beginning-of-the-script-to-prevent-unbound-variable-error-300x75.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p style=\"text-align: justify;\">The above snapshot is a clear indication of an unbound variable error that dictates that by using the <strong>\u2018set -u\u2019 command <\/strong>you can easily identify and fix the issues during Bash scripting.<\/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, you have understood why unbound variable error occurs and what to do to prevent this issue. Wrapping up, if you understand the crucial steps of unbound variable errors perspicuously, these will help you address and fix all the potential issues and write maintainable Bash scripts.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"People_Also_Ask\"><\/span>People Also Ask<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"su-accordion su-u-trim\">\n<div class=\"su-spoiler su-spoiler-style-fancy su-spoiler-icon-plus\" data-scroll-offset=\"0\" data-anchor-in-url=\"no\"><div class=\"su-spoiler-title\" tabindex=\"0\" role=\"button\"><span class=\"su-spoiler-icon\"><\/span><strong>Can I disable the \u2018nounset\u2019 option in Bash?<\/strong><\/div><div class=\"su-spoiler-content su-u-clearfix su-u-trim\">\n<p><strong>Yes<\/strong>, you can disable the \u2018nounset\u2019 option by using the <strong>\u2018set +u\u2019<\/strong> command in Bash.<\/p>\n<\/div><\/div>\n<div class=\"su-spoiler su-spoiler-style-fancy su-spoiler-icon-plus su-spoiler-closed\" data-scroll-offset=\"0\" data-anchor-in-url=\"no\"><div class=\"su-spoiler-title\" tabindex=\"0\" role=\"button\"><span class=\"su-spoiler-icon\"><\/span><strong>Are there any risks while disabling the 'nounset' option in Bash scripts?<\/strong><\/div><div class=\"su-spoiler-content su-u-clearfix su-u-trim\">\n<p style=\"text-align: justify;\"><strong>Yes<\/strong>, If you use any undefined variables, then disabling the &#8216;nounset&#8217; option can lead to unintended potential issues in Bash scripts.<\/p>\n<\/div><\/div>\n<div class=\"su-spoiler su-spoiler-style-fancy su-spoiler-icon-plus su-spoiler-closed\" data-scroll-offset=\"0\" data-anchor-in-url=\"no\"><div class=\"su-spoiler-title\" tabindex=\"0\" role=\"button\"><span class=\"su-spoiler-icon\"><\/span><strong>How to exit the script when encountered an unbound variable in Bash?<\/strong><\/div><div class=\"su-spoiler-content su-u-clearfix su-u-trim\">\n<p style=\"text-align: justify;\">Use the command \u2018set -u\u2019 and enable the \u2018nounset\u2019 option. This will automatically treat the unbound variables as errors and take you out of the script.<\/p>\n<\/div><\/div>\n<div class=\"su-spoiler su-spoiler-style-fancy su-spoiler-icon-plus su-spoiler-closed\" data-scroll-offset=\"0\" data-anchor-in-url=\"no\"><div class=\"su-spoiler-title\" tabindex=\"0\" role=\"button\"><span class=\"su-spoiler-icon\"><\/span><strong>Does Bash allow to customize the error messages for unbound variable errors?<\/strong><\/div><div class=\"su-spoiler-content su-u-clearfix su-u-trim\">\n<p style=\"text-align: justify;\"><strong>No<\/strong>, Bash doesn\u2019t allow direct customization of error messages for unbound variable errors, but you can redirect the output of the standard error in a custom log file to handle the errors according to your needs.<\/p>\n<\/div><\/div>\n<\/div>\n<p><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\/scope\/local-variable\/\" target=\"_blank\" rel=\"noopener\">Insights to Local Variable in Bash [4 Practical Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/global-variable\/\" target=\"_blank\" rel=\"noopener\">A Total Guide on Global Variable in Bash [3 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;Variable Scopes in Bash&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/\" target=\"_blank\" rel=\"noopener\">Variable Scopes in Bash<\/a><\/span>\u00a0|\u00a0<span data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Bash Variables&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&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;23913&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;How Unbound Variable Error Occurs in Bash [Cases \\u0026amp; Solutions]&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>There may be a situation when you want to use a variable, but it\u2019s not declared, assigned or initialized. In &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"How Unbound Variable Error Occurs in Bash [Cases &#038; Solutions]\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/#more-23913\" aria-label=\"Read more about How Unbound Variable Error Occurs in Bash [Cases &#038; Solutions]\">Read more<\/a><\/p>\n","protected":false},"author":314905,"featured_media":23916,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[163],"class_list":["post-23913","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-scripting","tag-variable-scope","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>How Unbound Variable Error Occurs in Bash [Cases &amp; Solutions] - LinuxSimply<\/title>\n<meta name=\"description\" content=\"Always ensure to define and initialize variables in your scripts to prevent potential issues due to unbound variable error in Bash.\" \/>\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\/scope\/unbound-variable\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How Unbound Variable Error Occurs in Bash [Cases &amp; Solutions] - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"Always ensure to define and initialize variables in your scripts to prevent potential issues due to unbound variable error in Bash.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-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-10-04T11:28:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-23T05:38:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Feature-image-Bash-unbound-variable-1.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=\"9 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\\\/scope\\\/unbound-variable\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/unbound-variable\\\/\"},\"author\":{\"name\":\"Nadiba Rahman\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/69b717a03a5d9dc48e683aaffe22d33f\"},\"headline\":\"How Unbound Variable Error Occurs in Bash [Cases &#038; Solutions]\",\"datePublished\":\"2023-10-04T11:28:18+00:00\",\"dateModified\":\"2024-01-23T05:38:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/unbound-variable\\\/\"},\"wordCount\":1712,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/unbound-variable\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/Feature-image-Bash-unbound-variable-1.png\",\"keywords\":[\"variable scope\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/unbound-variable\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/unbound-variable\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/unbound-variable\\\/\",\"name\":\"How Unbound Variable Error Occurs in Bash [Cases & Solutions] - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/unbound-variable\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/unbound-variable\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/Feature-image-Bash-unbound-variable-1.png\",\"datePublished\":\"2023-10-04T11:28:18+00:00\",\"dateModified\":\"2024-01-23T05:38:05+00:00\",\"description\":\"Always ensure to define and initialize variables in your scripts to prevent potential issues due to unbound variable error in Bash.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/unbound-variable\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/unbound-variable\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/unbound-variable\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/Feature-image-Bash-unbound-variable-1.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/Feature-image-Bash-unbound-variable-1.png\",\"width\":400,\"height\":400,\"caption\":\"Feature image-Bash unbound variable\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/unbound-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\":\"Variable Scopes in Bash\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"How Unbound Variable Error Occurs in Bash [Cases &#038; Solutions]\"}]},{\"@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":"How Unbound Variable Error Occurs in Bash [Cases & Solutions] - LinuxSimply","description":"Always ensure to define and initialize variables in your scripts to prevent potential issues due to unbound variable error in Bash.","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\/scope\/unbound-variable\/","og_locale":"en_US","og_type":"article","og_title":"How Unbound Variable Error Occurs in Bash [Cases & Solutions] - LinuxSimply","og_description":"Always ensure to define and initialize variables in your scripts to prevent potential issues due to unbound variable error in Bash.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/nadiba.rahman?mibextid=ZbWKwL","article_published_time":"2023-10-04T11:28:18+00:00","article_modified_time":"2024-01-23T05:38:05+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Feature-image-Bash-unbound-variable-1.png","type":"image\/png"}],"author":"Nadiba Rahman","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Nadiba Rahman","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/"},"author":{"name":"Nadiba Rahman","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/69b717a03a5d9dc48e683aaffe22d33f"},"headline":"How Unbound Variable Error Occurs in Bash [Cases &#038; Solutions]","datePublished":"2023-10-04T11:28:18+00:00","dateModified":"2024-01-23T05:38:05+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/"},"wordCount":1712,"commentCount":0,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Feature-image-Bash-unbound-variable-1.png","keywords":["variable scope"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/","name":"How Unbound Variable Error Occurs in Bash [Cases & Solutions] - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Feature-image-Bash-unbound-variable-1.png","datePublished":"2023-10-04T11:28:18+00:00","dateModified":"2024-01-23T05:38:05+00:00","description":"Always ensure to define and initialize variables in your scripts to prevent potential issues due to unbound variable error in Bash.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Feature-image-Bash-unbound-variable-1.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Feature-image-Bash-unbound-variable-1.png","width":400,"height":400,"caption":"Feature image-Bash unbound variable"},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-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":"Variable Scopes in Bash","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/"},{"@type":"ListItem","position":5,"name":"How Unbound Variable Error Occurs in Bash [Cases &#038; Solutions]"}]},{"@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\/23913","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=23913"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/23913\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/23916"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=23913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=23913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=23913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}