{"id":24399,"date":"2023-10-10T04:23:20","date_gmt":"2023-10-10T04:23:20","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=24399"},"modified":"2024-04-18T04:14:42","modified_gmt":"2024-04-18T04:14:42","slug":"bash-local-variable","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/local-variable\/","title":{"rendered":"Insights to Local Variable in Bash [4 Practical Examples]"},"content":{"rendered":"<p style=\"text-align: justify;\"><strong>Local variables<\/strong> are crucial elements in Bash scripting. These specify the variables that are used to store and manipulate data within a<strong> limited scope (a specific function or code block)<\/strong>. In this article, I\u2019m going to demonstrate what a local variable is and how it performs different practical tasks and maintains data integrity in Bash. Let\u2019s dive into it!<\/p>\n<div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Practice-Files_Bash-Local-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 to Use Local 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' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/local-variable\/#What_is_a_Local_Variable_in_Bash\" >What is a Local Variable in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/local-variable\/#Why_Use_Bash_Local_Variable\" >Why Use Bash Local Variable?<\/a><\/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\/local-variable\/#4_Examples_of_Local_Variable_Implementation_in_Bash\" >4 Examples of Local Variable Implementation 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\/local-variable\/#Example_1_Data_Processing_Manipulation_Using_Local_Variable_in_Bash\" >Example 1: Data Processing &amp; Manipulation Using Local 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\/local-variable\/#Example_2_Temporary_File_Operation_Using_Local_Variable_in_Bash\" >Example 2: Temporary File Operation Using Local 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\/scope\/local-variable\/#Example_3_Loop_Iteration_Using_Local_Variable_in_Bash\" >Example 3: Loop Iteration Using Local Variable in Bash<\/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\/scope\/local-variable\/#Example_4_Error_Handling_Using_Local_Variable_in_Bash\" >Example 4: Error Handling Using Local 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-8\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/local-variable\/#Conclusion\" >Conclusion<\/a><\/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\/scope\/local-variable\/#People_Also_Ask\" >People Also Ask<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"What_is_a_Local_Variable_in_Bash\"><\/span>What is a Local Variable in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">A local variable is nothing but a variable that is declared and utilized within a defined scope, such as within a block of code or function. These variables are designed with a confined visibility and you can access them within a bounded scope in which they are defined.<\/p>\n<p style=\"text-align: justify;\">Local variables help encapsulate data, avoid naming conflicts, maintain high data integrity and refine program collocation in <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/introduction\/what-is-bash-scripting\/\" target=\"_blank\" rel=\"noopener\"><strong>Bash scripts<\/strong><\/a>. Typically, these variables don\u2019t interfere with the variables in global scope having the same name.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Why_Use_Bash_Local_Variable\"><\/span>Why Use Bash Local Variable?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">No doubt, local variables aid in promoting code structure and reducing unintended bugs in Bash scripts. Here are some core facts about why one should use local variables in Bash:<\/p>\n<ul>\n<li><strong>Allow Data Encapsulation<\/strong><\/li>\n<\/ul>\n<p style=\"text-align: justify;\">Local variables allow you to encapsulate data effectively within a specific scope. Specially, when you need to store any temporary data that are only relevant within a part of your code, directly employ local variables with no hesitation.<\/p>\n<ul>\n<li><strong>Prevent Unintended Conflicts<\/strong><\/li>\n<\/ul>\n<p style=\"text-align: justify;\">Local variables are only accessible from the defined scopes within a function which ensures that the value of the local variable is only confined to that specific function and it doesn\u2019t affect the outside of the function. This helps reduce the risk of naming conflicts and prevent unintended changes to global variables in wider scopes.<\/p>\n<ul>\n<li><strong>Reduce Global Clutter<\/strong><\/li>\n<\/ul>\n<p style=\"text-align: justify;\">Too many global variables result in <strong>clutter <\/strong>and <strong>global pollution<\/strong> making it hard to keep the right track of the declared variables throughout the script. In this effort, local variables are the ones that reduce the risk of global pollution with unnecessary variables.<\/p>\n<ul>\n<li><strong>Avoid Parallel Interference<\/strong><\/li>\n<\/ul>\n<p style=\"text-align: justify;\">Using local variables helps avoid <strong>parallel interference<\/strong> when writing scripts that involve multiple concurrent processes running in the background. As local variables are confined only to a specific function within the script, each process operates independently and doesn\u2019t affect the variables in other processes.<\/p>\n<ul>\n<li><strong>Enhance Security<\/strong><\/li>\n<\/ul>\n<p style=\"text-align: justify;\">Local variables ensure sensitive data protection by limiting its visibility to a local context. While dealing with a variable that contains sensitive data like an API key or user password, using local variables is crucial for minimizing its scope and reducing the risk of unexpected bugs or accidental exposure.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"4_Examples_of_Local_Variable_Implementation_in_Bash\"><\/span>4 Examples of Local Variable Implementation in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In Bash scripting, local variables can be used for various purposes like data processing, temporary file operation, loop iteration, error handling and so on within functions. The following section is a clear illustration of such examples of local variable implementation in Bash:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Example_1_Data_Processing_Manipulation_Using_Local_Variable_in_Bash\"><\/span>Example 1: Data Processing &amp; Manipulation Using Local Variable in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">You can use <strong>local variables<\/strong> to <strong>process and manipulate data<\/strong> within a function. The following is a step-by-step interpretation of data processing using <strong>string substitution<\/strong> that describes how local variables can manipulate data within the local scope of a function. Let\u2019s see the 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 data.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>data.sh<\/strong>: This is a script. Here, I have named the script <strong>\u2018data.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-24412\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img1-Opening-the-file-in-Nano-text-editor-2.png\" alt=\"Opening the file in Nano text editor\" width=\"629\" height=\"157\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img1-Opening-the-file-in-Nano-text-editor-2.png 629w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img1-Opening-the-file-in-Nano-text-editor-2-300x75.png 300w\" sizes=\"(max-width: 629px) 100vw, 629px\" \/><\/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 (data.sh) &gt;<\/strong><\/span><\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\ndata() {\n\u00a0 local data=&quot;$1&quot;\n\u00a0 #Manipulating the local variable\n\u00a0 local new_data=&quot;${data\/\/Windows\/Linux}&quot; #Performing string substitution\n\u00a0 echo &quot;Data after processing: $new_data&quot;\n}\ninput=&quot;HelloWindows&quot;\n\n#Calling the function\ndata &quot;$input&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 <strong>#!\/bin\/bash<\/strong>, \u2018<strong>#!<\/strong>\u2019 is called <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/basics\/writing\/bin-bash-script-and-shebang\/\" target=\"_blank\" rel=\"noopener\">\u2018<strong>Shebang<\/strong>\u2019 or \u2018<strong>Hashbang<\/strong>\u2019<\/a>. Here, the script defines a function called <strong>\u2018data\u2019 <\/strong>and declares a variable with a single input data assignment. Then, it manipulates data by performing string substitution where all occurrences of <strong>\u2018Windows\u2019 <\/strong>are replaced with <strong>\u2018Linux\u2019 <\/strong>and stores the result in a new local variable. Finally, the script displays the processed data by employing the <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/input-output\/output\/echo-command\/\" target=\"_blank\" rel=\"noopener\"><strong>echo command<\/strong><\/a>.<\/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 data.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>data.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-24413\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img2-Adding-executable-permission-to-the-script-2.png\" alt=\"Adding executable permission to the script\" width=\"700\" height=\"173\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img2-Adding-executable-permission-to-the-script-2.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img2-Adding-executable-permission-to-the-script-2-300x74.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/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=\"\">.\/data.sh<\/code><\/pre>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-24414\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img3-Output-of-data-processing-using-local-variable.png\" alt=\"Output of data processing using local variable\" width=\"700\" height=\"205\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img3-Output-of-data-processing-using-local-variable.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img3-Output-of-data-processing-using-local-variable-300x88.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>From the image, you can see that the data in the script has been processed using the local variable.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Example_2_Temporary_File_Operation_Using_Local_Variable_in_Bash\"><\/span>Example 2: Temporary File Operation Using Local Variable in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">For any kind of <strong>temporary file operations<\/strong>, you can use local variables. Generally, temporary files are used for temporary data storage where local variables specify a particular scope of function that holds the specific path of the file and other related operations. Here\u2019s 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 Example 1<\/strong><\/a>, to save &amp; make the script executable.<\/div><\/div>\n<p><span style=\"color: #800080;\"><strong>Script (temp.sh) &gt;<\/strong><\/span><\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\ntemp_file() {\n\u00a0 local temporary_file\n\u00a0 #Creating a temporary file\n\u00a0 temporary_file=$(mktemp)\n\u00a0 #Removing the temporary file\n\u00a0 rm &quot;$temporary_file&quot;\n\u00a0 echo &quot;Removed the temporary file: $temporary_file&quot;\n}\n\n#Calling the function\ntemp_file<\/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 declares a function called <strong>\u2018temp_file\u2019 <\/strong>and creates a temporary file within it using the <strong>\u2018mktemp\u2019 <\/strong>command. Then, it removes the created temporary file promptly by using the <a href=\"https:\/\/linuxsimply.com\/rm-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>\u2018rm\u2019 command<\/strong><\/a>. Finally, the script echoes a message as an indication of temporary file removal.<\/p>\n<\/div><\/div>\n<p>Now, run the script by the following command:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">.\/temp.sh<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-24415\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img4-Output-of-removing-temporary-file-using-local-variable.png\" alt=\"Output of removing temporary file using local variable\" width=\"700\" height=\"181\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img4-Output-of-removing-temporary-file-using-local-variable.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img4-Output-of-removing-temporary-file-using-local-variable-300x78.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>From the image, you can see that the temporary file has been removed by employing a local variable.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Example_3_Loop_Iteration_Using_Local_Variable_in_Bash\"><\/span>Example 3: Loop Iteration Using Local Variable in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">While working with <strong>loops <\/strong>within a function, it\u2019s very easy to use local variables to iterate through the arrays and operate each element. A simple demonstration is given below:<\/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 Example 1<\/strong><\/a>, to save &amp; make the script executable.<\/div><\/div>\n<p><span style=\"color: #800080;\"><strong>Script (loop.sh) &gt;<\/strong><\/span><\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\niterate_loop() {\n\u00a0 local array=(&quot;$@&quot;)\n\u00a0 #Looping through the array\n\u00a0 for item in &quot;${array[@]}&quot;; do\n\u00a0\u00a0\u00a0 echo &quot;Processing the item: $item&quot;\n\u00a0 done\n}\narray_items=(&quot;Linux&quot; &quot;Windows&quot; &quot;Mac&quot;)\n\n#Calling the function\niterate_loop &quot;${array_items[@]}&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 defines a function <strong>\u2018iterate_loop\u2019 <\/strong>expecting <strong>an array<\/strong> as its argument. Within the function, the script declares a local variable and assigns the array to it and then a <strong>\u2018for\u2019 loop<\/strong> is used to iterate through the array elements. Finally, it displays the outputs using the <strong>echo command<\/strong>.<\/p>\n<\/div><\/div>\n<p>Now, run the script by the following command:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">.\/loop.sh<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-24416\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img5-Output-of-loop-iteration-using-local-variable.png\" alt=\"Output of loop iteration using local variable\" width=\"685\" height=\"227\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img5-Output-of-loop-iteration-using-local-variable.png 685w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img5-Output-of-loop-iteration-using-local-variable-300x99.png 300w\" sizes=\"(max-width: 685px) 100vw, 685px\" \/><\/p>\n<p>The above image dictates how you can employ local variables within loops to iterate through array elements.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Example_4_Error_Handling_Using_Local_Variable_in_Bash\"><\/span>Example 4: Error Handling Using Local Variable in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Availing local variables for <strong>error handling<\/strong> within a function is an effective and organized process as the variable\u2019s scope remains limited to that specific function only. 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 Example 1<\/strong><\/a>, to save &amp; make the script executable.<\/div><\/div>\n<p><span style=\"color: #800080;\"><strong>Script (error.sh) &gt;<\/strong><\/span><\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\ntrigger_error() {\n\u00a0 local error=&quot;$1&quot;\n\u00a0 echo &quot;Error message: $error&quot;\n}\n\n#Simulating an error condition\nif [ ! -d \/home\/nadiba\/color ]; then\n\u00a0 trigger_error &quot;Directory not found.&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;\">This script defines a function called <strong>\u2018trigger_error\u2019 <\/strong>that awaits a single argument. Within the function, the script declares a local variable and assigns the first argument passed to the function to it. Lastly, the script simulates an <strong>error condition<\/strong> and uses the <strong>echo command<\/strong> 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=\"\">.\/error.sh<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-24417\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img6-Output-of-error-handling-using-local-variable.png\" alt=\"Output of error handling using local variable\" width=\"700\" height=\"198\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img6-Output-of-error-handling-using-local-variable.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/img6-Output-of-error-handling-using-local-variable-300x85.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p style=\"text-align: justify;\">The above image depicts an error message as an indication of the error-handling operation using a local variable within a function.<\/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;\">Wrapping up, <strong>local variables<\/strong> are useful for managing data within particular scopes, controlling data integrity and availing consistent and assembled Bash scripting practices.<\/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 two functions have local variables with the same name in a Bash script?<\/strong><\/div><div class=\"su-spoiler-content su-u-clearfix su-u-trim\">\n<p><strong>Yes<\/strong>, two functions can have local variables with the same name in a Bash 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 it require specifying the data type while declaring a local variable in Bash?<\/strong><\/div><div class=\"su-spoiler-content su-u-clearfix su-u-trim\">\n<p style=\"text-align: justify;\"><strong>No<\/strong>, as variables are <strong>dynamically typed<\/strong> in Bash, it doesn\u2019t require specifying the data type while declaring a local variable.<\/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 local variables in Bash case-sensitive?<\/strong><\/div><div class=\"su-spoiler-content su-u-clearfix su-u-trim\">\n<p><strong>Yes<\/strong>, local variables in Bash are case-sensitive.<\/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>Can recursive functions in Bash use local variables?<\/strong><\/div><div class=\"su-spoiler-content su-u-clearfix su-u-trim\">\n<p style=\"text-align: justify;\"><strong>Yes<\/strong>, <strong>recursive functions (functions that call themselves to perform a task)<\/strong> in Bash can use local variables.<\/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\/global-variable\/\" target=\"_blank\" rel=\"noopener\">A Total Guide on Global Variable in Bash [3 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/unbound-variable\/\" target=\"_blank\" rel=\"noopener\">How Unbound Variable Error Occurs in Bash [Cases &amp; Solutions]<\/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;24399&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;1&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&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;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;Insights to Local Variable in Bash [4 Practical Examples]&quot;,&quot;width&quot;:&quot;142.5&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: 142.5px;\">\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            5\/5 - (1 vote)    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Local variables are crucial elements in Bash scripting. These specify the variables that are used to store and manipulate data &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Insights to Local Variable in Bash [4 Practical Examples]\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/local-variable\/#more-24399\" aria-label=\"Read more about Insights to Local Variable in Bash [4 Practical Examples]\">Read more<\/a><\/p>\n","protected":false},"author":314905,"featured_media":24401,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[163],"class_list":["post-24399","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>Insights to Local Variable in Bash [4 Practical Examples] - LinuxSimply<\/title>\n<meta name=\"description\" content=\"Use local variable in Bash to enhance data integrity, mitigate the security risks and make reliable and organized scripts.\" \/>\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\/local-variable\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Insights to Local Variable in Bash [4 Practical Examples] - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"Use local variable in Bash to enhance data integrity, mitigate the security risks and make reliable and organized scripts.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/local-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-10T04:23:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-18T04:14:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Feature-image-Bash-local-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\\\/scope\\\/local-variable\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/local-variable\\\/\"},\"author\":{\"name\":\"Nadiba Rahman\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/69b717a03a5d9dc48e683aaffe22d33f\"},\"headline\":\"Insights to Local Variable in Bash [4 Practical Examples]\",\"datePublished\":\"2023-10-10T04:23:20+00:00\",\"dateModified\":\"2024-04-18T04:14:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/local-variable\\\/\"},\"wordCount\":1507,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/local-variable\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/Feature-image-Bash-local-variable.png\",\"keywords\":[\"variable scope\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/local-variable\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/local-variable\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/local-variable\\\/\",\"name\":\"Insights to Local Variable in Bash [4 Practical Examples] - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/local-variable\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/local-variable\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/Feature-image-Bash-local-variable.png\",\"datePublished\":\"2023-10-10T04:23:20+00:00\",\"dateModified\":\"2024-04-18T04:14:42+00:00\",\"description\":\"Use local variable in Bash to enhance data integrity, mitigate the security risks and make reliable and organized scripts.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/local-variable\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/local-variable\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/local-variable\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/Feature-image-Bash-local-variable.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/Feature-image-Bash-local-variable.png\",\"width\":400,\"height\":400,\"caption\":\"Feature image-Bash local variable\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/scope\\\/local-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\":\"Insights to Local Variable in Bash [4 Practical Examples]\"}]},{\"@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":"Insights to Local Variable in Bash [4 Practical Examples] - LinuxSimply","description":"Use local variable in Bash to enhance data integrity, mitigate the security risks and make reliable and organized scripts.","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\/local-variable\/","og_locale":"en_US","og_type":"article","og_title":"Insights to Local Variable in Bash [4 Practical Examples] - LinuxSimply","og_description":"Use local variable in Bash to enhance data integrity, mitigate the security risks and make reliable and organized scripts.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/local-variable\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/nadiba.rahman?mibextid=ZbWKwL","article_published_time":"2023-10-10T04:23:20+00:00","article_modified_time":"2024-04-18T04:14:42+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Feature-image-Bash-local-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\/scope\/local-variable\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/local-variable\/"},"author":{"name":"Nadiba Rahman","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/69b717a03a5d9dc48e683aaffe22d33f"},"headline":"Insights to Local Variable in Bash [4 Practical Examples]","datePublished":"2023-10-10T04:23:20+00:00","dateModified":"2024-04-18T04:14:42+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/local-variable\/"},"wordCount":1507,"commentCount":0,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/local-variable\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Feature-image-Bash-local-variable.png","keywords":["variable scope"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/local-variable\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/local-variable\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/local-variable\/","name":"Insights to Local Variable in Bash [4 Practical Examples] - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/local-variable\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/local-variable\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Feature-image-Bash-local-variable.png","datePublished":"2023-10-10T04:23:20+00:00","dateModified":"2024-04-18T04:14:42+00:00","description":"Use local variable in Bash to enhance data integrity, mitigate the security risks and make reliable and organized scripts.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/local-variable\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/local-variable\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/local-variable\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Feature-image-Bash-local-variable.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Feature-image-Bash-local-variable.png","width":400,"height":400,"caption":"Feature image-Bash local variable"},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/local-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":"Insights to Local Variable in Bash [4 Practical Examples]"}]},{"@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\/24399","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=24399"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/24399\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/24401"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=24399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=24399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=24399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}