{"id":35177,"date":"2024-01-28T07:55:37","date_gmt":"2024-01-28T07:55:37","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=35177"},"modified":"2024-03-17T04:42:29","modified_gmt":"2024-03-17T04:42:29","slug":"bash-for-loop-with-variable","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/","title":{"rendered":"How to Use Bash \u201cfor\u201d Loop with Variable [12 Examples]"},"content":{"rendered":"<p style=\"text-align: justify;\">In the world of bash scripting, <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/\" target=\"_blank\" rel=\"noopener\"><strong>loops<\/strong><\/a> are like <strong>building blocks<\/strong>, helping to do things <strong>automatically<\/strong> and making tasks easier. One special tool in these loops is the &#8220;for&#8221; loop, and it&#8217;s great for efficient iteration through a set of values. You need to know how to use variables in a loop for precise control over the script. This article will dive into the various examples of utilizing the Bash &#8220;for&#8221; loop with variables.<\/p>\n<div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/Practice-Files-for-the-Bash-for-Loop-with-Variable.zip\" class=\"su-button su-button-style-default\" style=\"color:#000000;background-color:#E8F9FF;border-color:#bac8cc;border-radius:56px\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"color:#000000;padding:8px 26px;font-size:20px;line-height:30px;border-color:#effbff;border-radius:56px;text-shadow:0px 0px 0px #000000\"><i class=\"sui sui-download\" style=\"font-size:20px;color:#1AA1D6\"><\/i> Practice Files for the Bash \u201cfor\u201d Loop with Variable<\/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\/loop\/for-loop\/for-loop-with-variable\/#12_Examples_of_Bash_%E2%80%9Cfor%E2%80%9D_Loop_with_Variable\" >12 Examples of Bash \u201cfor\u201d Loop with Variable<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/#Example_1_Print_All_Array_Elements\" >Example 1: Print All Array Elements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/#Example_2_Calculate_the_Sum_of_Numbers\" >Example 2: Calculate the Sum of Numbers<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/#Example_3_Print_Array_of_Strings\" >Example 3: Print Array of Strings<\/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\/loop\/for-loop\/for-loop-with-variable\/#Example_4_Print_Words_in_a_Sentence\" >Example 4: Print Words in a Sentence<\/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\/loop\/for-loop\/for-loop-with-variable\/#Example_5_Check_OddEven_Numbers\" >Example 5: Check Odd\/Even Numbers<\/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\/loop\/for-loop\/for-loop-with-variable\/#Example_6_Check_Variable_in_For_Loop\" >Example 6: Check Variable in For Loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/#Example_7_Iterate_with_a_Counter\" >Example 7: Iterate with a Counter<\/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\/loop\/for-loop\/for-loop-with-variable\/#Example_8_Read_Variables_from_a_File\" >Example 8: Read Variables from a File<\/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\/loop\/for-loop\/for-loop-with-variable\/#Example_9_Count_Lines_from_Files\" >Example 9: Count Lines from Files<\/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\/loop\/for-loop\/for-loop-with-variable\/#Example_10_Process_All_Files_in_a_Directory\" >Example 10: Process All Files in a Directory<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/#Example_11_List_All_Running_Processes\" >Example 11: List All Running Processes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/#Example_12_Multiplication_Table_5%C3%9710\" >Example 12: Multiplication Table (5&#215;10)<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/#People_Also_Ask\" >People Also Ask<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/#What_is_a_loop_in_Bash\" >What is a loop in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/#How_do_I_run_a_for_loop_in_Linux\" >How do I run a for loop in Linux?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"12_Examples_of_Bash_%E2%80%9Cfor%E2%80%9D_Loop_with_Variable\"><\/span>12 Examples of Bash \u201cfor\u201d Loop with Variable<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Loop variables in Bash scripting play a fundamental role in executing <strong>repetitive tasks<\/strong> by dynamically adapting to values during each iteration. Whether iterating through <strong>arrays of numbers, strings, or processing files<\/strong>, the loop variable provides a flexible mechanism for accessing and handling different elements. In this article, I will go through some of the common examples of using variables in the <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/\" target=\"_blank\" rel=\"noopener\"><strong>for loop<\/strong><\/a>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Example_1_Print_All_Array_Elements\"><\/span>Example 1: Print All Array Elements<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To print all <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/array-variable\/\" target=\"_blank\" rel=\"noopener\"><strong>array<\/strong><\/a> elements, use a \u201cfor\u201d loop to go through all elements of the array. Run the following bash script to do so:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Define an array of numbers\nnumbers=(1 2 3 4 5)\n\nfor num in &quot;${numbers[@]}&quot;; do\necho &quot;Number: $num&quot;\ndone<\/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>This Bash script begins by defining an array named numbers containing the values 1, 2, 3, 4, and 5. Subsequently, a &#8220;for&#8221; loop is initiated, iterating through each element in the numbers array. The loop variable num dynamically takes on the value of each array element during each iteration. Within the loop, the script utilizes the <a href=\"https:\/\/linuxsimply.com\/echo-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>echo command<\/strong><\/a> to print the current value of num alongside the string &#8220;Number:&#8221;.<\/p>\n<\/div><\/div>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-35188 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/1.-Print-All-Array-Elements.png\" alt=\"Print All Array Elements\" width=\"825\" height=\"234\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/1.-Print-All-Array-Elements.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/1.-Print-All-Array-Elements-300x85.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/1.-Print-All-Array-Elements-768x218.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">Here, all array elements are echoed on separate lines.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Example_2_Calculate_the_Sum_of_Numbers\"><\/span>Example 2: Calculate the Sum of Numbers<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Calculating the <strong>sum<\/strong> of some <strong>predefined numbers<\/strong> is a very common task in Bash scripting. The script uses a &#8220;for&#8221; loop to iterate through the numbers of a <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/basics\/examples\/list-in-bash\/\" target=\"_blank\" rel=\"noopener\"><strong>list<\/strong><\/a> and adds them to a <strong>running total<\/strong>. After the loop is complete, the total sum is echoed. Execute this script to calculate the sum of numbers:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nsum=0\n\n# Iterate over a list of numbers\nfor number in 1 2 3 4 5\ndo\n# Add the current number to the sum\nsum=$((sum + number))\ndone\n\n# Print the sum after the loop\necho &quot;Sum: $sum&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>This Bash script initializes a variable sum to zero. It then employs a &#8220;for&#8221; loop to iterate through a list of numbers. Within the loop, the current number is added to the sum variable using the <code class=\"\" data-line=\"\">((sum + number))<\/code> syntax. As the loop iterates through each number, the sum accumulates their values. This script effectively calculates the sum of the specified numbers and stores the result in the sum variable. After the loop is complete it echoes the total sum.<\/p>\n<\/div><\/div>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-35189 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/2.-Calculate-the-Sum-of-Numbers.png\" alt=\"The Sum of Numbers in the list are calculated\" width=\"825\" height=\"140\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/2.-Calculate-the-Sum-of-Numbers.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/2.-Calculate-the-Sum-of-Numbers-300x51.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/2.-Calculate-the-Sum-of-Numbers-768x130.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">The sum of all the numbers on the list is echoed.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Example_3_Print_Array_of_Strings\"><\/span>Example 3: Print Array of Strings<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Working with arrays in Bash scripting allows for efficiently handling <strong>multiple similar values<\/strong>. This example introduces a bash script that iterates through an array of <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/\" target=\"_blank\" rel=\"noopener\"><strong>strings<\/strong><\/a>, specifically fruit names, and then prints each fruit along with a label as shown below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Define an array of strings\nfruits=(&quot;Apple&quot; &quot;Banana&quot; &quot;Orange&quot; &quot;Grapes&quot;)\n\nfor fruit in &quot;${fruits[@]}&quot;; do\necho &quot;Fruit: $fruit&quot;\ndone<\/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>This Bash script defines an array called fruits with strings representing different fruits. Using a &#8220;for&#8221; loop, it iterates through each element in the fruits array, assigning the current fruit to the variable fruit during each iteration. Within the loop, the script uses the <code class=\"\" data-line=\"\">echo<\/code> command to print each fruit alongside the string &#8220;Fruit:&#8221;.<\/p>\n<\/div><\/div>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-35190 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/3.-Print-Array-of-Strings.png\" alt=\"Array of Strings are printed\" width=\"825\" height=\"167\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/3.-Print-Array-of-Strings.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/3.-Print-Array-of-Strings-300x61.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/3.-Print-Array-of-Strings-768x155.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Example_4_Print_Words_in_a_Sentence\"><\/span>Example 4: Print Words in a Sentence<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Efficiently breaking down a sentence into individual words is a common need in Bash scripting. To print all words of a sentence separately in individual lines use a &#8220;for&#8221; loop to iterate over the sentence as demonstrated in the following script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nsentence=&quot;Bash scripting is great for automation&quot;\n\nfor word in $sentence; do\necho &quot;Word: $word&quot;\ndone<\/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>This Bash script defines a variable sentence containing the string &#8220;Bash scripting is great for automation.&#8221; It then utilizes a &#8220;for&#8221; loop to <strong>iterate<\/strong> through <strong>each word<\/strong> in the sentence, with the loop variable word representing each <strong>space-separated word<\/strong>. The script uses the <code class=\"\" data-line=\"\">echo<\/code> command to print each word preceded by the string &#8220;Word:&#8221;.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-35191 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/4.-Words-in-a-Sentence.png\" alt=\"All Words in the Sentence are printed\" width=\"825\" height=\"225\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/4.-Words-in-a-Sentence.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/4.-Words-in-a-Sentence-300x82.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/4.-Words-in-a-Sentence-768x209.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">Here all words of the sentence are printed on separate lines.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Example_5_Check_OddEven_Numbers\"><\/span>Example 5: Check Odd\/Even Numbers<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Analyzing and <strong>classifying<\/strong> numbers as <strong>odd or even<\/strong> is a fundamental aspect of programming logic. This example presents a bash script that prompts the user to enter a number, validates the input, and then uses <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/\" target=\"_blank\" rel=\"noopener\"><strong>\u201cif\u201d conditional statements<\/strong><\/a> and a &#8220;for&#8221; loop to determine if each number from 1 to the user&#8217;s input is odd or even. To do so execute the script below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\necho &quot;Enter a number: &quot;\nread user_input\n\n# Check if the input is a valid number\nif ! [[ $user_input =~ ^[0-9]+$ ]]; then\necho &quot;Invalid input. Please enter a valid number.&quot;\nexit 1\nfi\n\n# Check if the number is greater than 0\nif (( user_input &lt;= 0 )); then\necho &quot;Invalid input. Please enter a positive integer greater than 0.&quot;\nexit 1\nfi\n\n# Use a for loop to iterate through a range\nfor (( i=1; i&lt;=$user_input; i++ )); do\n# Check if the current number is odd or even\nif (( i % 2 == 0 )); then\necho &quot;$i is even&quot;\nelse\necho &quot;$i is odd&quot;\nfi\ndone<\/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>This Bash script <strong>prompts<\/strong> the user to <strong>input a number<\/strong>, checks if the input is a valid positive integer and then uses a &#8220;for&#8221; loop to iterate through a range from 1 to the user-input number. Within the loop, it checks whether each number is odd or even using a conditional statement and prints the result. The script is a basic example of user input validation and demonstrates the determination of odd and even numbers in a specified range.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-35192 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/5.-Check-Odd-or-Even-Numbers.png\" alt=\"Check all Odd\/Even Numbers until the input number\" width=\"825\" height=\"258\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/5.-Check-Odd-or-Even-Numbers.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/5.-Check-Odd-or-Even-Numbers-300x94.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/5.-Check-Odd-or-Even-Numbers-768x240.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">Here, all integers from zero to the given input number 5 are checked and echoed as even or odd.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Example_6_Check_Variable_in_For_Loop\"><\/span>Example 6: Check Variable in For Loop<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To check if some variables are present in the bash script use a <strong>conditional statement<\/strong> within the \u201cfor\u201d loop. This example showcases a bash script that iterates through a <strong>numerical range<\/strong>, creating variable names based on the iteration index. Inside the loop, it checks for the existence of these variables using the <code class=\"\" data-line=\"\">[[ -v $variable ]]<\/code> command. Run the following script to do so:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nnumber_3=10\n\nfor i in {1..5}; do\nvariable=&quot;number_$i&quot;\n# Checking if the variable exists\nif [[ -v $variable ]]; then\necho &quot;$variable exists.&quot;\nelse\necho &quot;$variable does not exist.&quot;\nfi\ndone<\/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>This bash script uses a \u201cfor\u201d loop to iterate through a range of numerical values from 1 to 5. Inside the loop, it creates variables with numerical suffixes, such as number_1, number_2, and so on. The script then checks for the existence of each variable using the <code class=\"\" data-line=\"\">[[ -v $variable ]]<\/code> command and prints a message indicating whether the variable exists or not.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-35193 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/6.-Check-Variable-in-For-Loop.png\" alt=\"Check Variable in For Loop\" width=\"825\" height=\"194\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/6.-Check-Variable-in-For-Loop.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/6.-Check-Variable-in-For-Loop-300x71.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/6.-Check-Variable-in-For-Loop-768x181.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">Here only the number_3 variable exists in the script and that is also shown in the output.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Example_7_Iterate_with_a_Counter\"><\/span>Example 7: Iterate with a Counter<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><strong>Counter<\/strong> is a variable used to keep track of the number of times a particular event or operation occurs. It is often employed in loops to control the iteration process. The following script initializes a counter variable (count) to 1 and continues the C-styled \u201cfor\u201d loop while the counter is less than or equal to 5 showcasing the efficient use of a counter variable in a loop structure:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nfor ((count=1; count&lt;=5; count++)); do\necho &quot;Iteration $count&quot;\ndone<\/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>This bash script utilizes a \u201cfor\u201d loop to iterate through a numerical range from 1 to 5. Within the loop, it echoes a message indicating the current iteration number, denoted by the variable \u201ccount\u201d. The loop structure initializes \u201ccount\u201d to 1, continues while the count is less than or equal to 5, and increments the count in each iteration.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-35194 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/7.-Iterate-with-a-Counter.png\" alt=\"Iterate with a Counter\" width=\"825\" height=\"199\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/7.-Iterate-with-a-Counter.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/7.-Iterate-with-a-Counter-300x72.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/7.-Iterate-with-a-Counter-768x185.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">Here using a C-styled for loop 1 to 5 is counted.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Example_8_Read_Variables_from_a_File\"><\/span>Example 8: Read Variables from a File<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Reading and <strong>processing data from a text file<\/strong> is important for task automation in Bash scripting. This example showcases a bash script that reads the user\u2019s name and age from a text file using a for loop variable and prints them in a formatted way.<\/p>\n<p style=\"text-align: justify;\">Content of the cat names_and_ages.txt text file:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">Alice 25\nBob 30\nCharlie 22\nDiana 28<\/code><\/pre>\n<p style=\"text-align: justify;\">Run the following script to read data from a text file in Bash shell:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Read each line of the text file into an array\nlines=($(cat names_and_ages.txt))\n\n# Iterate over the array\nfor ((i = 0; i &lt; ${#lines[@]}; i += 2)); do\nname=${lines[i]}\nage=${lines[i + 1]}\necho &quot;Name: $name, Age: $age&quot;\ndone<\/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>This bash script reads each line from a text file named \u201cnames_and_ages.txt\u201d into an array named lines. It then uses a for loop to iterate through the array, extract values of name and age from consecutive array elements, and then store them in variables \u201cname\u201d and \u201cage\u201d respectively. Then it echoes a formatted message for each pair displaying the name and age in the specified format.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-35195 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/8.-Read-Variables-from-a-File.png\" alt=\"Read Variables from a File\" width=\"825\" height=\"167\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/8.-Read-Variables-from-a-File.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/8.-Read-Variables-from-a-File-300x61.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/8.-Read-Variables-from-a-File-768x155.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">Here all data was read from the text file and then the formatted data was echoed.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Example_9_Count_Lines_from_Files\"><\/span>Example 9: Count Lines from Files<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The \u201cfor\u201d loop can be used to <strong>count the number of lines<\/strong> in each file with the <strong>same extension<\/strong> within the current directory. The following script uses a &#8220;for&#8221; loop to iterate through the matching files, echoing a message for each file being processed. Inside the loop, the <code class=\"\" data-line=\"\">wc -l<\/code> command, coupled with <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/\" target=\"_blank\" rel=\"noopener\"><strong>redirection<\/strong><\/a> <code class=\"\" data-line=\"\">&lt;<\/code>, counts and displays the number of lines in the current file. Run the following bash script to count the number of lines in each file with the \u201c.sh\u201d file extension within the current directory:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nfor file in *.sh\ndo\necho &quot;Processing file: $file&quot;\nline_count=$(wc -l &lt; &quot;$file&quot;)\u00a0 # Count lines in each file\necho &quot;This file has $line_count lines.&quot;\ndone<\/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>This bash script uses a for loop to iterate through all files in the current directory with a \u201c.sh\u201d extension. For each file, it echoes a message indicating the name of the file being processed and then uses the <code class=\"\" data-line=\"\">wc -l<\/code> command to count and display the number of lines in that file.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-35196 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/9.-Count-Lines-from-Files.png\" alt=\"Count number of Lines from all Files\" width=\"825\" height=\"707\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/9.-Count-Lines-from-Files.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/9.-Count-Lines-from-Files-300x257.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/9.-Count-Lines-from-Files-768x658.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">This script goes through all files with the \u201c.sh\u201d extension from the current directory and counts the number of lines of that file.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Example_10_Process_All_Files_in_a_Directory\"><\/span>Example 10: Process All Files in a Directory<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Iterating through files within a directory is a fundamental operation often used in shell scripting. The following script utilizes a &#8220;for&#8221; loop in conjunction with <strong>parameter expansion<\/strong> to iterate through the list of files in the current directory. Here $file is an example of parameter expansion. During the execution of the script, the variable &#8220;file&#8221; undergoes expansion to its current value in each iteration within the &#8220;for&#8221; loop. Execute the following command to list all files in the current directory:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\necho &quot;Files in the current directory:&quot;\n\nfor file in *; do\necho &quot;$file&quot;\ndone<\/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>This script begins by echoing a message indicating that it will list files in the current directory. It then uses a for loop to iterate through all files in the current directory echoing each filename in a new line. This also shows all hidden files in the current directory.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-35197 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/10.-Process-All-Files-in-a-Directory.png\" alt=\"list All Files in a Directory\" width=\"825\" height=\"482\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/10.-Process-All-Files-in-a-Directory.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/10.-Process-All-Files-in-a-Directory-300x175.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/10.-Process-All-Files-in-a-Directory-768x449.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">This script shows <strong>all files including the hidden ones<\/strong> from the current directory into the terminal.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Example_11_List_All_Running_Processes\"><\/span>Example 11: List All Running Processes<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Knowing the <strong>active processes<\/strong> proves essential for <strong>system monitoring and management<\/strong>. This example showcases a technique that leverages the <a href=\"https:\/\/linuxsimply.com\/ps-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>ps<\/strong><\/a>, <a href=\"https:\/\/linuxsimply.com\/awk-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>awk<\/strong><\/a>, and <a href=\"https:\/\/linuxsimply.com\/tail-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>tail<\/strong><\/a> commands within a for loop to generate a comprehensive list of running processes on a Unix-like system. Execute the following script to list all processes running on the system:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\necho &quot;Processes running on the system:&quot;\n\nfor process in $(ps aux | awk &#039;{print $11}&#039; | tail -n +2); do\necho &quot;$process&quot;\ndone<\/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>This bash script starts with echoing a message indicating that it will list all the processes running on the system. Then it utilizes a for loop to iterate over the names of processes obtained by running the <code class=\"\" data-line=\"\">ps aux<\/code> command. The <code class=\"\" data-line=\"\">awk<\/code> command extracts the 11th column which corresponds to the command or process name. Then the tail <code class=\"\" data-line=\"\">-n +2<\/code> command skips the header line from the \u201cps\u201d command\u2019s output. Finally, the script echoes each process name on a new line.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-35198 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/11.-List-All-Running-Processes.png\" alt=\"List All Running Processes\" width=\"825\" height=\"705\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/11.-List-All-Running-Processes.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/11.-List-All-Running-Processes-300x256.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/11.-List-All-Running-Processes-768x656.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">The names of all processes running on my system are shown on the terminal.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Example_12_Multiplication_Table_5%C3%9710\"><\/span>Example 12: Multiplication Table (5&#215;10)<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">A <strong>multiplication table<\/strong> is a mathematical table used to define a multiplication operation for an algebraic system. This script showcases a Bash implementation to generate a formatted multiplication table with a size of 5&#215;10 within the shell interface, using <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/nested-for-loop\/\" target=\"_blank\" rel=\"noopener\"><strong>nested \u201cfor\u201d loops<\/strong><\/a> and formatting techniques for visual clarity. Run the following script to create a 5&#215;10 multiplication table:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\necho &quot;Multiplication Table:&quot;\n\n# Print header\necho -e &quot;\u00a0 | \\c&quot;\nfor ((i=1; i&lt;=10; i++)); do\nprintf &quot;%-4s&quot; &quot;$i&quot;\ndone\necho\n\n# Print separator\necho &quot;-------------------------------------------&quot;\n\n# Print table\nfor ((i=1; i&lt;=5; i++)); do\necho -n &quot;$i | &quot;\nfor ((j=1; j&lt;=10; j++)); do\nproduct=$((i * j))\nprintf &quot;%-4s&quot; &quot;$product&quot;\ndone\necho\ndone<\/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>This Bash script generates a formatted multiplication table up to 5&#215;10. The script starts by echoing a message indicating that it will display a multiplication table. The header row is then printed with column labels, and a separator line is added for clarity.<\/p>\n<p>The main part of the script utilizes nested for loops to calculate and format the product of each multiplication. The outer loop controls the multiplicand values (i) from 1 to 5, while the inner loop handles the multiplier values (j) from 1 to 10. Each multiplication <code class=\"\" data-line=\"\">i*j<\/code> product is calculated and echoed on the same line with a space delimiter using the <code class=\"\" data-line=\"\">printf<\/code> command. The final output will look like a 5&#215;10 multiplication table.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-35199 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/12.-Multiplication-Table.png\" alt=\"(5x10) Multiplication Table\" width=\"825\" height=\"321\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/12.-Multiplication-Table.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/12.-Multiplication-Table-300x117.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/12.-Multiplication-Table-768x299.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">The output shows a 5 by 10 multiplication table with proper alignment.<\/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;\">In conclusion, getting the hang of the <strong>variable<\/strong> in &#8220;for loop&#8221; in Bash opens up a world of possibilities. It makes your scripting adventures more efficient and enjoyable. I hope this article helps you to get some solid idea about how to use variable in bash for loop.<\/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<h3><span class=\"ez-toc-section\" id=\"What_is_a_loop_in_Bash\"><\/span>What is a loop in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">In Bash scripting, a &#8220;loop&#8221; is a powerful construct that enables the repetitive execution of a series of commands, making it a fundamental element for <strong>automation<\/strong> and <strong>task iteration<\/strong>. Loops play a crucial role in simplifying and optimizing code, allowing developers to efficiently perform tasks without the need for redundant or excessive manual input.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_run_a_for_loop_in_Linux\"><\/span>How do I run a for loop in Linux?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To run a for loop in Linux, use the following simple syntax:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">for item in list; do\n# Commands to be executed for each item\ndone<\/code><\/pre>\n<p style=\"text-align: justify;\">Here, replace &#8220;list&#8221; with the items you want to iterate over. This loop structure is invaluable for automating repetitive tasks, making it an essential tool in Linux scripting.<\/p>\n<p><script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": {\n    \"@type\": \"Question\",\n    \"name\": \"What is a loop in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"In Bash scripting, a \\\"loop\\\" is a powerful construct that enables the repetitive execution of a series of commands, making it a fundamental element for automation and task iteration. Loops play a crucial role in simplifying and optimizing code, allowing developers to efficiently perform tasks without the need for redundant or excessive manual input.\"\n    }\n  }\n}\n<\/script><\/p>\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\/loop\/for-loop\/for-loop-example\/\" target=\"_blank\" rel=\"noopener\">10 Common Bash \u201cfor\u201d Loop Examples [Basic to Intermediate]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/\" target=\"_blank\" rel=\"noopener\">How to Iterate Through List Using \u201cfor\u201d Loop in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/increment-variable\/\" target=\"_blank\" rel=\"noopener\">Bash Increment and Decrement Variable in \u201cfor\u201d Loop<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/parallel-for-loop\/\" target=\"_blank\" rel=\"noopener\">How to Use Bash Parallel \u201cfor\u201d Loop [7 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-array\/\" target=\"_blank\" rel=\"noopener\">How to Loop Through Array Using \u201cfor\u201d Loop in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/\" target=\"_blank\" rel=\"noopener\">How to Use Bash \u201cfor\u201d Loop with Range [5 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/bash-seq\/\" target=\"_blank\" rel=\"noopener\">How to Use Bash \u201cfor\u201d Loop with \u201cseq\u201d Command [10 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/files\/\" target=\"_blank\" rel=\"noopener\">How to Use &#8220;for&#8221; Loop in Bash Files [9 Practical Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/directory\/\" target=\"_blank\" rel=\"noopener\">Usage of \u201cfor\u201d Loop in Bash Directory [5 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/one-line\/\" target=\"_blank\" rel=\"noopener\">How to Use \u201cfor\u201d Loop in One Line in Bash [7 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/infinite-loop\/\" target=\"_blank\" rel=\"noopener\">How to Create Infinite Loop in Bash [7 Cases]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-continue\/\" target=\"_blank\" rel=\"noopener\">How to Use Bash Continue with \u201cfor\u201d Loop [9 Examples]<\/a><\/strong><\/li>\n<\/ul>\n<hr \/>\n<p><strong>&lt;&lt; Go Back to <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;For Loop 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\/loop\/for-loop\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/\" target=\"_blank\" rel=\"noopener\">For Loop in Bash<\/a> |\u00a0<\/span><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Loops 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\/loop\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/\" target=\"_blank\" rel=\"noopener\">Loops in Bash<\/a><\/span> | <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Bash Scripting Tutorial&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\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\" target=\"_blank\" rel=\"noopener\">Bash Scripting Tutorial<\/a><\/span><\/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;35177&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;7&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 - (7 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;How to Use Bash \u201cfor\u201d Loop with Variable [12 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 - (7 votes)    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>In the world of bash scripting, loops are like building blocks, helping to do things automatically and making tasks easier. &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"How to Use Bash \u201cfor\u201d Loop with Variable [12 Examples]\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/#more-35177\" aria-label=\"Read more about How to Use Bash \u201cfor\u201d Loop with Variable [12 Examples]\">Read more<\/a><\/p>\n","protected":false},"author":314909,"featured_media":35200,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[175],"class_list":["post-35177","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-scripting","tag-for-loop","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 to Use Bash \u201cfor\u201d Loop with Variable [12 Examples] - LinuxSimply<\/title>\n<meta name=\"description\" content=\"How to use variable in the &quot;for&quot; loop for precise control over the script and task automation in the Bash scripting.\" \/>\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\/loop\/for-loop\/for-loop-with-variable\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Use Bash \u201cfor\u201d Loop with Variable [12 Examples] - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"How to use variable in the &quot;for&quot; loop for precise control over the script and task automation in the Bash scripting.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/\" \/>\n<meta property=\"og:site_name\" content=\"LinuxSimply\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/ridoychandra.shil.9\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-28T07:55:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-17T04:42:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/bash-for-loop-with-variable-feature-image.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=\"Ridoy Chandra Shil\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ridoy Chandra Shil\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 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\\\/loop\\\/for-loop\\\/for-loop-with-variable\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-with-variable\\\/\"},\"author\":{\"name\":\"Ridoy Chandra Shil\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/94793d6ad5334004f864932ed89e58b4\"},\"headline\":\"How to Use Bash \u201cfor\u201d Loop with Variable [12 Examples]\",\"datePublished\":\"2024-01-28T07:55:37+00:00\",\"dateModified\":\"2024-03-17T04:42:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-with-variable\\\/\"},\"wordCount\":2365,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-with-variable\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/bash-for-loop-with-variable-feature-image.png\",\"keywords\":[\"for loop\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-with-variable\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-with-variable\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-with-variable\\\/\",\"name\":\"How to Use Bash \u201cfor\u201d Loop with Variable [12 Examples] - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-with-variable\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-with-variable\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/bash-for-loop-with-variable-feature-image.png\",\"datePublished\":\"2024-01-28T07:55:37+00:00\",\"dateModified\":\"2024-03-17T04:42:29+00:00\",\"description\":\"How to use variable in the \\\"for\\\" loop for precise control over the script and task automation in the Bash scripting.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-with-variable\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-with-variable\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-with-variable\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/bash-for-loop-with-variable-feature-image.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/bash-for-loop-with-variable-feature-image.png\",\"width\":400,\"height\":400,\"caption\":\"How to use variable in the \\\"for\\\" loop for precise control over the script and task automation in the Bash scripting.\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-with-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\":\"Loops in Bash\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"For Loop in Bash\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"How to Use Bash \u201cfor\u201d Loop with Variable [12 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\\\/94793d6ad5334004f864932ed89e58b4\",\"name\":\"Ridoy Chandra Shil\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Ridoy-Chandra-Shil-II-96x96.png\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Ridoy-Chandra-Shil-II-96x96.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Ridoy-Chandra-Shil-II-96x96.png\",\"caption\":\"Ridoy Chandra Shil\"},\"description\":\"Hello everyone. I am Ridoy Chandra Shil, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Biomedical Engineering graduate from Bangladesh University of Engineering and Technology. I am a science and tech enthusiast. In my free time, I read articles about new tech and watch documentaries on science-related topics. I am also a big fan of \u201cThe Big Bang Theory\u201d. Read Full Bio\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/ridoychandra.shil.9\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/ridoy-chandra-shil\\\/\"],\"url\":\"https:\\\/\\\/linuxsimply.com\\\/author\\\/ridoy\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Use Bash \u201cfor\u201d Loop with Variable [12 Examples] - LinuxSimply","description":"How to use variable in the \"for\" loop for precise control over the script and task automation in the Bash scripting.","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\/loop\/for-loop\/for-loop-with-variable\/","og_locale":"en_US","og_type":"article","og_title":"How to Use Bash \u201cfor\u201d Loop with Variable [12 Examples] - LinuxSimply","og_description":"How to use variable in the \"for\" loop for precise control over the script and task automation in the Bash scripting.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/ridoychandra.shil.9","article_published_time":"2024-01-28T07:55:37+00:00","article_modified_time":"2024-03-17T04:42:29+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/bash-for-loop-with-variable-feature-image.png","type":"image\/png"}],"author":"Ridoy Chandra Shil","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Ridoy Chandra Shil","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/"},"author":{"name":"Ridoy Chandra Shil","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/94793d6ad5334004f864932ed89e58b4"},"headline":"How to Use Bash \u201cfor\u201d Loop with Variable [12 Examples]","datePublished":"2024-01-28T07:55:37+00:00","dateModified":"2024-03-17T04:42:29+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/"},"wordCount":2365,"commentCount":0,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/bash-for-loop-with-variable-feature-image.png","keywords":["for loop"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/","name":"How to Use Bash \u201cfor\u201d Loop with Variable [12 Examples] - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/bash-for-loop-with-variable-feature-image.png","datePublished":"2024-01-28T07:55:37+00:00","dateModified":"2024-03-17T04:42:29+00:00","description":"How to use variable in the \"for\" loop for precise control over the script and task automation in the Bash scripting.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/bash-for-loop-with-variable-feature-image.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/bash-for-loop-with-variable-feature-image.png","width":400,"height":400,"caption":"How to use variable in the \"for\" loop for precise control over the script and task automation in the Bash scripting."},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-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":"Loops in Bash","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/"},{"@type":"ListItem","position":4,"name":"For Loop in Bash","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/"},{"@type":"ListItem","position":5,"name":"How to Use Bash \u201cfor\u201d Loop with Variable [12 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\/94793d6ad5334004f864932ed89e58b4","name":"Ridoy Chandra Shil","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Ridoy-Chandra-Shil-II-96x96.png","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Ridoy-Chandra-Shil-II-96x96.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Ridoy-Chandra-Shil-II-96x96.png","caption":"Ridoy Chandra Shil"},"description":"Hello everyone. I am Ridoy Chandra Shil, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Biomedical Engineering graduate from Bangladesh University of Engineering and Technology. I am a science and tech enthusiast. In my free time, I read articles about new tech and watch documentaries on science-related topics. I am also a big fan of \u201cThe Big Bang Theory\u201d. Read Full Bio","sameAs":["https:\/\/www.facebook.com\/ridoychandra.shil.9","https:\/\/www.linkedin.com\/in\/ridoy-chandra-shil\/"],"url":"https:\/\/linuxsimply.com\/author\/ridoy\/"}]}},"_links":{"self":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/35177","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\/314909"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/comments?post=35177"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/35177\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/35200"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=35177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=35177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=35177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}