{"id":35301,"date":"2024-01-28T09:32:04","date_gmt":"2024-01-28T09:32:04","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=35301"},"modified":"2024-03-31T05:32:07","modified_gmt":"2024-03-31T05:32:07","slug":"bash-range","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/","title":{"rendered":"How to Use Bash \u201cfor\u201d Loop with Range [5 Methods]"},"content":{"rendered":"<p style=\"text-align: justify;\">Bash scripting provides a variety of <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/\" target=\"_blank\" rel=\"noopener\"><strong>loops<\/strong><\/a>, and the \u201cfor\u201d loop stands out as a versatile tool for automating tasks. It iterates over a sequence of values, executing a block of code for each iteration. One particularly useful feature in Bash scripting is the ability to leverage ranges within the &#8220;for&#8221; loop, offering a convenient way to iterate over a specific set of values. This article will dive into the various methods of utilizing range in the Bash &#8220;for&#8221; loop.<\/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-Range.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 Range<\/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\/range\/#5_Methods_of_Using_Bash_%E2%80%9Cfor%E2%80%9D_Loop_with_Range\" >5 Methods of Using Bash \u201cfor\u201d Loop with Range<\/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\/range\/#1_Using_Brace_Expansion\" >1. Using Brace Expansion<\/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\/range\/#2_Using_the_%E2%80%9Cseq%E2%80%9D_Command\" >2. Using the \u201cseq\u201d Command<\/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\/range\/#3_Using_C-style_%E2%80%9Cfor%E2%80%9D_Loop_Syntax\" >3. Using C-style \u201cfor\u201d Loop Syntax<\/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\/range\/#4_Using_the_%E2%80%9Ceval%E2%80%9D_Command_with_Brace_Expansion\" >4. Using the \u201ceval\u201d Command with Brace Expansion<\/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\/range\/#5_Looping_Over_Array_Elements\" >5. Looping Over Array Elements<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/#Practice_Problems_on_Bash_%E2%80%9Cfor%E2%80%9D_Loop_with_Range\" >Practice Problems on Bash \u201cfor\u201d Loop with Range<\/a><\/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\/loop\/for-loop\/range\/#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\/loop\/for-loop\/range\/#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-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/#How_to_use_variables_in_for_loop_Bash\" >How to use variables in for loop Bash?<\/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\/range\/#How_to_iterate_through_numbers_in_Bash\" >How to iterate through numbers in Bash?<\/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\/range\/#What_is_a_loop_variable\" >What is a loop variable?<\/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\/range\/#How_to_do_math_in_Bash_script\" >How to do math in Bash script?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/#How_to_calculate_time_in_a_bash_script\" >How to calculate time in a bash script?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/#What_is_for_loop_syntax\" >What is for loop syntax?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"5_Methods_of_Using_Bash_%E2%80%9Cfor%E2%80%9D_Loop_with_Range\"><\/span>5 Methods of Using Bash \u201cfor\u201d Loop with Range<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Bash scripting offers a variety of methods for utilizing the <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/\" target=\"_blank\" rel=\"noopener\"><strong>for loop<\/strong><\/a>, each with its unique advantages. Whether you prefer the concise brace expansion, the versatility of the seq command, the clarity of C-style syntax, the dynamism of eval with brace expansion, or the simplicity of looping over array elements, Bash provides a solution for every scripting requirement. This guide will go through <strong>5<\/strong> different methods of using the range in the bash for loop.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_Using_Brace_Expansion\"><\/span>1. Using Brace Expansion<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/brace-expansion\/\" target=\"_blank\" rel=\"noopener\"><strong>Brace expansion<\/strong><\/a> is a powerful feature in Bash that lets you generate lists of filenames, numbers, or other text patterns within your commands. It provides a concise and readable syntax for generating sequences within loops. This feature simplifies the process of iterating through ranges of values, making scripts more efficient and easier to understand.<br \/>\nThe basic syntax of a for loop with brace expansions is as follows:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">for i in {START..END}\nfor i in {START..END..INCREMENT\/DECREMENT}<\/code><\/pre>\n<p style=\"text-align: justify;\">The following example showcases various types of bash ranges using brace expansion:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash \n\necho &quot;Default Increment&quot; \nfor i in {1..5} \n  do \n    echo &quot;$i&quot; \n  done \n\necho &quot;Increment&quot; \nfor i in {1..10..3} \n  do \n    echo &quot;$i&quot; \n  done \n\necho &quot;Decrement&quot; \nfor i in {10..1..3} \n  do \n    echo &quot;$i&quot; \n  done\n<\/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 utilizes brace expansion to create three distinct loops, each demonstrating different behaviors regarding incrementation and decrementation. It starts with the &#8220;Default Increment&#8221; loop, using the range <code class=\"\" data-line=\"\">{1..5}<\/code> for a default increment of 1, echoing values 1 to 5. The subsequent &#8220;Increment&#8221; loop uses <code class=\"\" data-line=\"\">{1..10..3}<\/code> to skip every two values, echoing &#8220;1 4 7 10&#8221;. Lastly, the &#8220;Decrement&#8221; loop echoes &#8220;10 7 4 1&#8221; using <code class=\"\" data-line=\"\">{10..1..3}<\/code>, demonstrating a reverse order with a customized decrement.<\/p>\n<\/div><\/div>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-35329 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/01.-Using-brace-expansion.png\" alt=\"dynamic range is generated using brace expansion\" width=\"668\" height=\"636\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/01.-Using-brace-expansion.png 668w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/01.-Using-brace-expansion-300x286.png 300w\" sizes=\"(max-width: 668px) 100vw, 668px\" \/><\/p>\n<p style=\"text-align: justify;\">Here, using the brace expansion first, 1-5 are echoed using default increment. Then from 1 to 10, the numbers are printed with an increment of 3. After that, the same thing is done in a reverse order with a decrement of 3.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"2_Using_the_%E2%80%9Cseq%E2%80%9D_Command\"><\/span>2. Using the \u201cseq\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/bash-seq\/#What_is_the_seq_Command_in_Bash\" target=\"_blank\" rel=\"noopener\"><strong>seq command<\/strong><\/a> in Bash is a specialized tool for <strong>generating sequences of numbers<\/strong>. It simplifies the process of defining ranges with specific start, end, and increment\/decrement values, providing more control over the iteration.<\/p>\n<p style=\"text-align: justify;\">To use the seq command to create ranges in the for loop, execute the following example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash \n\necho &quot;Default Increment&quot; \nfor i in $(seq 1 5) \n  do \n    echo &quot;$i&quot; \n  done \n\necho &quot;Increment&quot; \nfor i in $(seq 1 3 10) \n  do \n    echo &quot;$i&quot; \n  done \n\necho &quot;Decrement&quot; \nfor i in $(seq 10 -3 1) \n  do \n    echo &quot;$i&quot; \n  done\n<\/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 demonstrates the versatility of the seq command in generating sequences with different increments and decrements. It starts with &#8220;Default Increment&#8221;, echoing values from 1 to 5. The &#8220;Increment&#8221; loop skips every two values, echoing &#8220;1 4 7 10&#8221;. The &#8220;Decrement&#8221; loop counts down from 10 to 1, echoing &#8220;10 7 4 1&#8221;.<\/p>\n<\/div><\/div>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-35330 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/02.-Using-the-seq-command.png\" alt=\"the &quot;seq&quot; command is used to generate dynamic range within &quot;for&quot; loop \" width=\"676\" height=\"635\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/02.-Using-the-seq-command.png 676w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/02.-Using-the-seq-command-300x282.png 300w\" sizes=\"(max-width: 676px) 100vw, 676px\" \/><\/p>\n<p style=\"text-align: justify;\">Here, using the seq command 1 to 5 are echoed using default increment, 1 to 10 are echoed with an increment of 3, and 10 to 1 are echoed with a decrement of 3.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3_Using_C-style_%E2%80%9Cfor%E2%80%9D_Loop_Syntax\"><\/span>3. Using C-style \u201cfor\u201d Loop Syntax<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Bash scripts offer versatility in implementing loops, including the adoption of <strong>C-style for loop<\/strong> syntax. This syntax <code class=\"\" data-line=\"\">for (( initialization; condition; update ))<\/code> provides a concise and powerful way to define loops with precise control over the iteration process.<\/p>\n<p style=\"text-align: justify;\">To create a range using the C style for loop, follow the script below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash \n\necho &quot;Post Increment&quot; \nfor (( i = 1; i &lt;= 3; i++ )) \n  do \n    echo &quot;$i&quot; \n  done \n\necho &quot;Pre Increment&quot; \nfor (( i = 1; i &lt;= 3; ++i )) \n  do \n    echo &quot;$i&quot; \n  done \n\necho &quot;Increment by 3&quot; \nfor (( i = 1; i &lt;= 10; i += 3 )) \n  do \n    echo &quot;$i&quot; \n  done \n\necho &quot;Decrement by 3&quot; \nfor (( i = 10; i &gt;= 1; i -= 3 )) \n  do \n    echo &quot;$i&quot; \n  done\n<\/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 illustrates various C-style for loop scenarios. In the &#8220;<strong>Post Increment<\/strong>&#8221; section, a loop initializes i to 1 and echoes values until i reaches 5 using the post-increment operator (<code class=\"\" data-line=\"\">i++<\/code>). The &#8220;<strong>Pre Increment<\/strong>&#8221; section functions similarly but with the pre-increment operator (<code class=\"\" data-line=\"\">++i<\/code>). Both sections produce the output &#8220;1 2 3&#8221;.<\/p>\n<p>Moving to &#8220;<strong>Increment by 3<\/strong>&#8220;, the script initializes i to 1 and increments it by 3 in each iteration until i is no longer less than or equal to 10, resulting in the output &#8220;1 4 7 10\u201d. In the final &#8220;<strong>Decrement by 3<\/strong>&#8221; section, the script initializes i to 10 and decrements it by 3 in each iteration, echoing &#8220;10 7 4 1&#8221; until i is less than 1.<\/p>\n<\/div><\/div>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-35331 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/03.-Using-C-style-for-loop-syntax.png\" alt=\"Using C-style \u201cfor\u201d Loop bash range is generated\" width=\"678\" height=\"708\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/03.-Using-C-style-for-loop-syntax.png 678w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/03.-Using-C-style-for-loop-syntax-287x300.png 287w\" sizes=\"(max-width: 678px) 100vw, 678px\" \/><\/p>\n<p style=\"text-align: justify;\">Here, using C style for loop first, 1-3 are echoed using post-increment and pre-increment. Then 1 to 10 are echoed with an increment of 3 and the reverse is echoed with a decrement of 3.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"4_Using_the_%E2%80%9Ceval%E2%80%9D_Command_with_Brace_Expansion\"><\/span>4. Using the \u201ceval\u201d Command with Brace Expansion<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <strong>eval<\/strong> command is a powerful tool that lets you dynamically execute code by treating a string as a Bash command. Bash scripting provides flexibility to integrate the eval command in conjunction with the <strong>brace expansion<\/strong>. This combination enables the dynamic generation of sequences and variable-based range definitions within loops.<\/p>\n<p style=\"text-align: justify;\">The basic syntax of using the eval command with brace expansion to create a bash range is as shown below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">for i in $(eval echo {START..END})\nfor i in $(eval echo {START..END..INCREMENT\/DECREMENT})<\/code><\/pre>\n<p style=\"text-align: justify;\">The following example showcases various types of bash ranges using the eval command in conjunction with the brace expansion:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash \n\n# Dynamic Range Generation \nstart_value=2 \nend_value=7 \n\necho &quot;Dynamic Range Generation:&quot; \nfor i in $(eval echo &quot;{$start_value..$end_value}&quot;) \n  do \n    echo &quot;$i&quot; \n  done \n\n# Custom Increment\/Decrement \nstart_value=2 \nend_value=20 \nstep_size=4 \n\necho &quot;Looping with custom increment:&quot; \nfor i in $(eval echo &quot;{$start_value..$end_value..$step_size}&quot;) \n  do \n    echo &quot;$i&quot; \n  done\n<\/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 showcases the flexibility of eval with brace expansion for <strong>dynamic range generation<\/strong> and custom increment\/decrement loops. In the first part, it dynamically generates and echoes a sequence from 2 to 7. The second section demonstrates custom incrementing, generating a sequence from 2 to 20 with a step size of 4.<br \/>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-35332 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/04.-Using-the-eval-command-with-brace-expansion.png\" alt=\"bash range is dynamically generated Using the \u201ceval\u201d Command with Brace Expansion\" width=\"681\" height=\"525\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/04.-Using-the-eval-command-with-brace-expansion.png 681w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/04.-Using-the-eval-command-with-brace-expansion-300x231.png 300w\" sizes=\"(max-width: 681px) 100vw, 681px\" \/><\/p>\n<p style=\"text-align: justify;\">Here the eval command is used to dynamically print 2 to 7 and then print 2 to 18 with a custom increment of 4.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"5_Looping_Over_Array_Elements\"><\/span>5. Looping Over Array Elements<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">An <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/array-variable\/\" target=\"_blank\" rel=\"noopener\"><strong>array<\/strong><\/a> is a powerful data structure that offers a way to group and manage multiple values under a single name. Iterating over the elements of an array is a common operation in bash scripting.<\/p>\n<p style=\"text-align: justify;\">To understand how to use arrays to create ranges go through the script below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash \n\n# Define an array \nfruits=(&quot;Apple&quot; &quot;Banana&quot; &quot;Orange&quot; &quot;Grapes&quot;) \n\n# Loop to iterate over array elements \necho &quot;Looping over array elements:&quot; \nfor fruit in &quot;${fruits[@]}&quot; \n  do \n    echo &quot;Current fruit: $fruit&quot; \n  done\n<\/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 demonstrates a straightforward way to iterate over the elements of an array named &#8220;fruits&#8221;. The array is pre-defined with four elements, representing different types of fruits. The script initiates a for loop using the syntax for fruit in <code class=\"\" data-line=\"\">&quot;${fruits[@]}&quot;<\/code>\u00a0allowing it to iterate seamlessly over each element in the array. Within the loop, the script echoes the name of the current fruit for each iteration.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-35333 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/05.-Looping-over-array-elements.png\" alt=\"bash range is generated using array\" width=\"678\" height=\"230\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/05.-Looping-over-array-elements.png 678w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/05.-Looping-over-array-elements-300x102.png 300w\" sizes=\"(max-width: 678px) 100vw, 678px\" \/><\/p>\n<p style=\"text-align: justify;\">Here, an array is created and a for loop is used to iterate over all elements of the array.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Practice_Problems_on_Bash_%E2%80%9Cfor%E2%80%9D_Loop_with_Range\"><\/span>Practice Problems on Bash \u201cfor\u201d Loop with Range<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">To sharpen your skill in bash \u201cfor\u201d loop with range, try solving the problem using the concept discussed in this article:<\/p>\n<ol>\n<li style=\"text-align: justify;\">Write a Bash script to print the even numbers between 10 and 20 using a &#8220;for&#8221; loop.<\/li>\n<li style=\"text-align: justify;\">Implement a Bash script to calculate and print the square of numbers from 1 to 10 using a &#8220;for&#8221; loop.<\/li>\n<li style=\"text-align: justify;\">Write a Bash script to identify and print the prime numbers between 10 and 30 using a &#8220;for&#8221; loop.<\/li>\n<li style=\"text-align: justify;\">Create a Bash script to reverse a given string using the &#8220;for&#8221; loop and print the modified string.<\/li>\n<li style=\"text-align: justify;\">Write a Bash script to determine if a given number is a palindrome using the &#8220;for&#8221; loop.<\/li>\n<\/ol>\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, diving into the intricacies of the Bash &#8220;for&#8221; loop range has uncovered a realm of possibilities for efficient and dynamic scripting. The range, whether achieved through brace expansion, seq command, C-style syntax, or dynamic eval with brace expansion, provides scripters with a toolkit to address a wide spectrum of scenarios. I hope you find this article helpful.<\/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=\"How_to_use_variables_in_for_loop_Bash\"><\/span>How to use variables in for loop Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To use variables in a Bash &#8220;for&#8221; loop, you can employ various methods, such as brace expansion, seq command, C-style syntax, and dynamic eval with brace expansion. Following is a basic syntax for using a variable in the for loop:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">for variable in {start..end..increment} \n  do \n    # Your code here using $variable \n  done\n<\/code><\/pre>\n<p style=\"text-align: justify;\">Here, replace start, end, and increment with the appropriate values for your loop. The variable <code class=\"\" data-line=\"\">$variable<\/code> takes on each value in the specified range during each iteration of the loop.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_iterate_through_numbers_in_Bash\"><\/span>How to iterate through numbers in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To iterate through numbers using a &#8220;for&#8221; loop in bash use the basic syntax as follows:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">for num in &quot;${numbers[@]}&quot;; do \n  # Your code here, reference the current number as $num \ndone\n<\/code><\/pre>\n<p style=\"text-align: justify;\">Within the loop, you can perform various operations or execute commands using the current number represented by the variable <code class=\"\" data-line=\"\">$num<\/code>. This approach is particularly useful when you have a predefined list of numbers stored in an array.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_a_loop_variable\"><\/span>What is a loop variable?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">A loop variable is a variable used within the context of a programming loop, such as a &#8220;<strong>for<\/strong>&#8221; or &#8220;<strong>while<\/strong>&#8221; loop. It serves as a counter or iterator that takes on different values during each iteration of the loop. The loop variable is often utilized to control the flow of the loop, determining how many times the loop should execute or specifying the range of values it should iterate over.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_do_math_in_Bash_script\"><\/span>How to do math in Bash script?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">In Bash scripting, performing <strong>mathematical operations<\/strong> involves using <strong>arithmetic expressions<\/strong>. These expressions allow you to carry out various mathematical calculations, such as addition, subtraction, multiplication, and division, directly within your script. To initiate a mathematical operation, you can utilize the arithmetic expansion feature using the <strong>double parentheses<\/strong> <code class=\"\" data-line=\"\">(( ))<\/code> or the <strong>let<\/strong> command, the <strong>expr<\/strong> command, or the <a href=\"https:\/\/linuxsimply.com\/bc-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>bc command<\/strong><\/a>. For example, to add two numbers, you can use <code class=\"\" data-line=\"\">result=$((num1 + num2))<\/code> command or <code class=\"\" data-line=\"\">let result=num1+num2<\/code> command or <code class=\"\" data-line=\"\">result=$(expr $num1 + $num2)<\/code> or <code class=\"\" data-line=\"\">result=$(echo &quot;$num1 + $num2&quot; | bc)<\/code> command.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_calculate_time_in_a_bash_script\"><\/span>How to calculate time in a bash script?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To calculate the <strong>execution time<\/strong> of a bash script, you can use the <a href=\"https:\/\/linuxsimply.com\/date-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>date command<\/strong><\/a>. To do this first encapsulate your script between <code class=\"\" data-line=\"\">start_time=$(date +&quot;%s&quot;)<\/code> and <code class=\"\" data-line=\"\">end_time=$(date +&quot;%s&quot;)<\/code> commands. Then using the <code class=\"\" data-line=\"\">elapsed_time=$((end_time - start_time))<\/code> command the elapsed time is calculated. Then you can echo the value of the <code class=\"\" data-line=\"\">elapsed_time<\/code> variable into the terminal.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_for_loop_syntax\"><\/span>What is for loop syntax?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">In Bash, the syntax for a basic &#8220;for&#8221; loop is straightforward. Here is the basic syntax:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">for variable in sequence \n  do \n    # Commands to be performed in each iteration \n  done\n<\/code><\/pre>\n<p style=\"text-align: justify;\">You can use different types of sequences, such as ranges, arrays, or command substitutions, providing flexibility to the &#8220;for&#8221; loop. The commands or actions inside the loop block are executed for each iteration, with the loop variable taking on the values from the specified sequence.<\/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 variable?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"A loop variable is a variable used within the context of a programming loop, such as a \\\"for\\\" or \\\"while\\\" loop. It serves as a counter or iterator that takes on different values during each iteration of the loop. The loop variable is often utilized to control the flow of the loop, determining how many times the loop should execute or specifying the range of values it should iterate over.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How to do math in Bash script?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"In Bash scripting, performing mathematical operations involves using arithmetic expressions. These expressions allow you to carry out various mathematical calculations, such as addition, subtraction, multiplication, and division, directly within your script. To initiate a mathematical operation, you can utilize the arithmetic expansion feature using the double parentheses (( )) or the let command, the expr command, or the bc command. For example, to add two numbers, you can use result=$((num1 + num2)) command or let result=num1+num2 command or result=$(expr $num1 + $num2) or result=$(echo \\\"$num1 + $num2\\\" | bc) command.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How to calculate time in a bash script?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To calculate the execution time of a bash script, you can use the date command. To do this first encapsulate your script between start_time=$(date +\\\"%s\\\") and end_time=$(date +\\\"%s\\\") commands. Then using the elapsed_time=$((end_time - start_time)) command the elapsed time is calculated. Then you can echo the value of the elapsed_time variable into the terminal.\"\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\/for-loop-with-variable\/\" target=\"_blank\" rel=\"noopener\">How to Use Bash \u201cfor\u201d Loop with Variable [12 Examples]<\/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\/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\/nested-for-loop\/\" target=\"_blank\" rel=\"noopener\">How to Use Nested \u201cfor\u201d Loop 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;35301&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;6&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 - (6 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;How to Use Bash \u201cfor\u201d Loop with Range [5 Methods]&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 - (6 votes)    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bash scripting provides a variety of loops, and the \u201cfor\u201d loop stands out as a versatile tool for automating tasks. &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"How to Use Bash \u201cfor\u201d Loop with Range [5 Methods]\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/#more-35301\" aria-label=\"Read more about How to Use Bash \u201cfor\u201d Loop with Range [5 Methods]\">Read more<\/a><\/p>\n","protected":false},"author":314909,"featured_media":35334,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[175],"class_list":["post-35301","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 Range [5 Methods] - LinuxSimply<\/title>\n<meta name=\"description\" content=\"How to use the &quot;for&quot; loop in Bash scripting with range to create sequence with automatic or manual increment\/decrement in 5 different methods\" \/>\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\/range\/\" \/>\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 Range [5 Methods] - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"How to use the &quot;for&quot; loop in Bash scripting with range to create sequence with automatic or manual increment\/decrement in 5 different methods\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/\" \/>\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-28T09:32:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-31T05:32:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/bash-range-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=\"10 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\\\/range\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/range\\\/\"},\"author\":{\"name\":\"Ridoy Chandra Shil\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/94793d6ad5334004f864932ed89e58b4\"},\"headline\":\"How to Use Bash \u201cfor\u201d Loop with Range [5 Methods]\",\"datePublished\":\"2024-01-28T09:32:04+00:00\",\"dateModified\":\"2024-03-31T05:32:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/range\\\/\"},\"wordCount\":1798,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/range\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/bash-range-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\\\/range\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/range\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/range\\\/\",\"name\":\"How to Use Bash \u201cfor\u201d Loop with Range [5 Methods] - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/range\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/range\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/bash-range-feature-image.png\",\"datePublished\":\"2024-01-28T09:32:04+00:00\",\"dateModified\":\"2024-03-31T05:32:07+00:00\",\"description\":\"How to use the \\\"for\\\" loop in Bash scripting with range to create sequence with automatic or manual increment\\\/decrement in 5 different methods\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/range\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/range\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/range\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/bash-range-feature-image.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/bash-range-feature-image.png\",\"width\":400,\"height\":400,\"caption\":\"How to use the \\\"for\\\" loop in Bash scripting with range to create sequence with automatic or manual increment\\\/decrement in 5 different methods\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/range\\\/#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 Range [5 Methods]\"}]},{\"@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 Range [5 Methods] - LinuxSimply","description":"How to use the \"for\" loop in Bash scripting with range to create sequence with automatic or manual increment\/decrement in 5 different methods","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\/range\/","og_locale":"en_US","og_type":"article","og_title":"How to Use Bash \u201cfor\u201d Loop with Range [5 Methods] - LinuxSimply","og_description":"How to use the \"for\" loop in Bash scripting with range to create sequence with automatic or manual increment\/decrement in 5 different methods","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/ridoychandra.shil.9","article_published_time":"2024-01-28T09:32:04+00:00","article_modified_time":"2024-03-31T05:32:07+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/bash-range-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":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/"},"author":{"name":"Ridoy Chandra Shil","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/94793d6ad5334004f864932ed89e58b4"},"headline":"How to Use Bash \u201cfor\u201d Loop with Range [5 Methods]","datePublished":"2024-01-28T09:32:04+00:00","dateModified":"2024-03-31T05:32:07+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/"},"wordCount":1798,"commentCount":0,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/bash-range-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\/range\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/","name":"How to Use Bash \u201cfor\u201d Loop with Range [5 Methods] - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/bash-range-feature-image.png","datePublished":"2024-01-28T09:32:04+00:00","dateModified":"2024-03-31T05:32:07+00:00","description":"How to use the \"for\" loop in Bash scripting with range to create sequence with automatic or manual increment\/decrement in 5 different methods","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/bash-range-feature-image.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/bash-range-feature-image.png","width":400,"height":400,"caption":"How to use the \"for\" loop in Bash scripting with range to create sequence with automatic or manual increment\/decrement in 5 different methods"},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/#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 Range [5 Methods]"}]},{"@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\/35301","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=35301"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/35301\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/35334"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=35301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=35301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=35301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}