{"id":15680,"date":"2019-02-27T11:35:52","date_gmt":"2019-02-27T11:35:52","guid":{"rendered":"https:\/\/www.hostinger.com\/tutorials\/?p=15680"},"modified":"2025-12-22T15:18:54","modified_gmt":"2025-12-22T15:18:54","slug":"bash-for-loop","status":"publish","type":"post","link":"\/tutorials\/bash-for-loop","title":{"rendered":"How to use the bash for loop: Syntax and examples"},"content":{"rendered":"<p>A <strong>Bash for loop<\/strong> is a statement in the Bash programming language that allows a code or script to run repeatedly. It enables you to finish repetitive tasks simultaneously, which helps improve system management efficiency.&nbsp;<\/p><p>A Bash for loop works by assigning items in a list to a variable and executing an operation for each of them. The basic syntax is as follows:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for VARIABLE in 1 2 3 4 5 .. N\nPerform the below command:\ncommand1\ncommand2\ncommandN\ndone<\/pre><p>In a real-world scenario, a Bash for loop has various implementations, often involving other programming concepts. For example, you can use a Bash for loop to create a three-expression loop, an infinite loop, a start-and-stop sequence, and more.&nbsp;<\/p><p>Continue reading to learn more about a Bash for loop and its practical usage examples.&nbsp;<\/p><p class=\"has-text-align-center\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Bash-Cheat-Sheet.pdf\" target=\"_blank\" rel=\"noreferrer noopener\">Download comprehensive bash cheat sheet<\/a><\/p><p>\n\n\n\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-what-is-a-bash-for-loop\">What is a Bash for loop?<\/h2><p>A Bash for loop is a construction that lets you run a set of commands repeatedly on multiple items with a single execution. It is helpful for automating repetitive operations, such as renaming files, in a command-line-based system like a <a href=\"\/vps-hosting\">Linux virtual private server<\/a> (VPS). <\/p><p>The logic behind a Bash for loop is as follows: a set of commands will continue to run in a loop for each item listed in the variable. This loop ends after running the operation for the last item. <\/p><p>Simply put, you can interpret it as <strong>for<\/strong> each item assigned to the variable, <strong>loop<\/strong> the same set of commands. However, you can incorporate conditional statements to modify how the loop will run. <\/p><p>Let&rsquo;s explore the syntax of a Bash for loop line by line in the next section to better understand about how it works. <\/p><h2 class=\"wp-block-heading\" id=\"h-what-is-the-syntax-of-a-bash-for-loop\">What is the syntax of a Bash for loop?<\/h2><p>The Bash for loop executes a set of commands repeatedly, with the loop iterating through a sequence of items or values. The syntax of such an operation looks like this:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for VARIABLE in 1 2 3 4 5 .. N\nPerform the below command:\ncommand1\ncommand2\ncommandN\ndone<\/pre><p>In the real world, this syntax would look like the example below:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\nfor i in 1 2 3 4 5\ndo\necho \"Hello $i\"\ndone<\/pre><p>Executing the bash file will cause the following sequence:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Hello 1\nHello 2\nHello 3\nHello 4 \nHello 5<\/pre><p>Let&rsquo;s inspect each element:<\/p><ul class=\"wp-block-list\">\n<li><strong>#!\/bin\/bash &ndash;<\/strong> shows that the code is a bash script.<\/li>\n\n\n\n<li><strong>i<\/strong> &ndash; is a placeholder for a variable. Meanwhile, <strong>$i<\/strong> is the individual value of the variable. You can also write it as <strong>c\/$c<\/strong> or by any other name.<\/li>\n\n\n\n<li><strong>in<\/strong> &ndash; separates the variable and the items that follow.<\/li>\n\n\n\n<li><strong>1 2 3 4 5<\/strong> &ndash; is an example of items you want to perform the instruction on.<\/li>\n\n\n\n<li><strong>do<\/strong> &ndash; is the keyword that starts the loops. It will then execute the instruction <strong>n<\/strong> times, with <strong>n<\/strong> being the total number of items. Here, the value of <strong>n<\/strong> is <strong>5<\/strong>.<\/li>\n\n\n\n<li><strong>echo &ldquo;Hello: $i<strong>&ldquo;<\/strong><\/strong> &ndash; is the <a href=\"\/tutorials\/linux-commands\">Linux command<\/a> or operation we will repeat <strong>n<\/strong> times. Remember, quotation marks turn anything inside it into one variable.<\/li>\n\n\n\n<li><strong>done<\/strong> &ndash; stops the loop.<\/li>\n<\/ul><p>The other two common loop command syntaxes are this:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for VARIABLE in file1 file2 file3\ndo\ncommand1 on $VARIABLE\ncommand2\ncommandN\ndone<\/pre><p>And this:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for OUTPUT in $(Linux-Or-Unix-Command-Here)\ndo\ncommand1 on $OUTPUT\ncommand2 on $OUTPUT\ncommandN\ndone<\/pre><figure class=\"wp-block-image size-large\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-vps-hosting\" href=\"\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/public\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><p>Now that we understand the Bash for loop syntax, let&rsquo;s explore examples of how the actual script looks in real-life applications. <\/p><h2 class=\"wp-block-heading\" id=\"h-what-are-the-examples-of-a-bash-for-loop\"><strong>What are the examples of a Bash for loop?<\/strong><\/h2><p>Here are Bash for loop examples used to perform multiple operations. If you wish to follow along, you&rsquo;ll have to log into your VPS. If you&rsquo;re having trouble, read our <a href=\"\/tutorials\/how-to-use-putty-ssh\">Putty SSH tutorial<\/a> to learn more about how to do so. <\/p><p>Remember that bash functions need to be in a <a href=\"\/tutorials\/how-to-run-sh-file-in-linux\"><strong>.sh<\/strong> file<\/a>. To create one, run the following in the command line:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">vim filename.sh<\/pre><p>This will create a .sh file, and will open it in the VIM editor. You can learn more in the previously mentioned basic bash function article.<\/p><h3 class=\"wp-block-heading\" id=\"h-how-to-use-a-bash-for-loop-with-a-number\"><strong>How to use a Bash for loop with a number<\/strong><\/h3><p>Using a Bash for loop with numbers lets you iterate through a range instead of specifying the items individually. To do this, add the range in curly braces separated by double dots.<\/p><p>For example, the following loop will echo all numbers from one to five:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for i in {1..5}\ndo\n  echo \"$i\"\ndone<\/pre><p>You can also change the increment using the <strong>{START..END..INCREMENT}<\/strong> three-expression syntax. Here&rsquo;s the code example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for i in {1..10..2}\ndo\n  echo \"Number: $i\"\ndone<\/pre><p>\n\n\n<div><p class=\"important\"><strong>Important!<\/strong> In some scripts, the increment syntax uses double parentheses instead of curly braces. Regardless, both have the same function.<\/p><\/div>\n\n\n\n<\/p><p>The loop will operate on the first value of <strong>1<\/strong>, move up by two increments to <strong>3<\/strong>,<strong> <\/strong>and so on. Once it reaches the end value of <strong>10<\/strong>, the code will stop. Here&rsquo;s the output:<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-loop-with-increment-output.png\"><img decoding=\"async\" width=\"807\" height=\"138\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-loop-with-increment-output.png\/public\" alt=\"The bash for loop with increment output\" class=\"wp-image-94152\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-loop-with-increment-output.png\/w=807,fit=scale-down 807w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-loop-with-increment-output.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-loop-with-increment-output.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-loop-with-increment-output.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 807px) 100vw, 807px\" \/><\/a><\/figure><\/div><p>Note that the range feature is only available in <strong>Bash version 3.0<\/strong> or later, while the increment is supported in <strong>Bash 4.0<\/strong> and newer.<\/p><h3 class=\"wp-block-heading\" id=\"h-how-to-use-a-bash-for-loop-with-array-elements\"><strong>How to use a Bash for loop with array elements<\/strong><\/h3><p>Combining a Bash for loop with an array iterates over elements grouped together and executes a set of commands for each of them. Instead of using a list, <a href=\"\/tutorials\/bash-array\">incorporating arrays into Bash<\/a> makes your script more organized and easily readable, especially when you want to use many items.<\/p><p>To use a Bash for loop with an array, declare the array and its items at the beginning. Then, add it to your for-in expression like so:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#Declare an array of items\narray=(\"item1\" \"item2\" \"item3\" \"item4\")\n\n#Iterate through the array and apply the operations\nfor item in \"${array[@]}\"\ndo\n   command1\n   command2\n   command3\ndone\n<\/pre><p>Here&rsquo;s an example of a Bash for loop with array elements, using fruits as the items:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">fruit_array=(\"apple\" \"banana\" \"red cherry\" \"green grape\")\n\nfor fruit in \"${fruit_array[@]}\"\ndo\n   echo \"Fruit: $fruit\"\ndone<\/pre><p>The bash loop will iterate through items in the array and use the <strong>echo <\/strong>command to print them with the <strong>Fruit:<\/strong> prefix. This is what the output looks like:<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-loop-with-array-output.png\"><img decoding=\"async\" width=\"737\" height=\"112\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-loop-with-array-output.png\/public\" alt=\"The output of bash for loop with an array\" class=\"wp-image-94148\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-loop-with-array-output.png\/w=737,fit=scale-down 737w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-loop-with-array-output.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-loop-with-array-output.png\/w=150,fit=scale-down 150w\" sizes=\"(max-width: 737px) 100vw, 737px\" \/><\/a><\/figure><\/div><p>If you add another command, the loop will operate on the same item before moving to the next one. For example, we&rsquo;ll insert another <strong>echo <\/strong>to add a suffix to the item. Here&rsquo;s the output:<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-for-loop-with-array-and-two-operations-output.png\"><img decoding=\"async\" width=\"892\" height=\"201\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-for-loop-with-array-and-two-operations-output.png\/public\" alt=\"The output of a bash for loop with an array and two operations\" class=\"wp-image-94149\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-for-loop-with-array-and-two-operations-output.png\/w=892,fit=scale-down 892w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-for-loop-with-array-and-two-operations-output.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-for-loop-with-array-and-two-operations-output.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-for-loop-with-array-and-two-operations-output.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 892px) 100vw, 892px\" \/><\/a><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-how-to-use-a-bash-for-loop-with-a-shell-variable\"><strong>How to use a Bash for loop with a shell variable<\/strong><\/h3><p>Combining a Bash for loop with a shell variable enables you to store items that your code will iterate through. It works similarly to an array in that you group multiple elements, but it uses spaces to split the entries. <\/p><p>A Bash for loop with a shell variable uses the following syntax:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#Define the shell variable\nvariable=\"a single item\"\n#Iterate through the variable and apply the operations\nfor item in $variable\ndo\n   command1\n   command2\n   command3\ndone<\/pre><p>The shell variable only contains one data element, but the Bash loop automatically iterates through space-separated items, treating them as different entities. Consider this example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var_numbers=\"1 2 3 4 5\"\n\nfor number in $var_numbers\ndo\n   echo \"Number: $number\"\ndone<\/pre><p>Instead of printing the numbers as a string, the bash loop will print them individually because they are separated by a space. To treat the items as a single entity, enclose the <strong>$var_numbers<\/strong> variable in the <strong>for-in <\/strong>expression with quotation marks, like the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for number in \"$var_numbers\"<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/09\/bash-loop-with-single-item-variable-output.png\"><img decoding=\"async\" width=\"592\" height=\"46\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/09\/bash-loop-with-single-item-variable-output.png\/public\" alt=\"The output of a bash for loop with a single-item variable\" class=\"wp-image-94151\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/09\/bash-loop-with-single-item-variable-output.png\/w=592,fit=scale-down 592w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/09\/bash-loop-with-single-item-variable-output.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/09\/bash-loop-with-single-item-variable-output.png\/w=150,fit=scale-down 150w\" sizes=\"(max-width: 592px) 100vw, 592px\" \/><\/a><\/figure><\/div><p>You can change the behavior of Bash variables to change the loop output. To learn more about it, check out our <a href=\"\/tutorials\/bash-variables\">bash variables<\/a> tutorial.<\/p><h3 class=\"wp-block-heading\" id=\"h-how-to-use-a-bash-for-loop-with-strings\"><strong>How to use a Bash for loop with strings<\/strong><\/h3><p>Combining a Bash for loop with strings enables you to iterate through text for operations like <a href=\"\/tutorials\/bash-concatenate-strings\">concatenating<\/a>, which is typically grouped into a shell variable or array. Using a shell variable is common if your strings are not separated by a space, like the following syntax:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">variable=\"string1 string2 string3\"\nfor item in $variable\ndo\n   command1\n   command2\n   command3\ndone\n<\/pre><p>Meanwhile, use a Bash for loop with strings grouped into an array if your string contains whitespace. In addition to allowing the bash loop to read space-separated items, they are easier to iterate over and expand. Here&rsquo;s the syntax:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">array=(\"First item\" \"Second item\" \"Third item\" \"Fourth item\")\nfor item in \"${array[@]}\"\ndo\n   command1\n   command2\n   command3\ndone<\/pre><h3 class=\"wp-block-heading\" id=\"h-how-to-use-a-bash-for-loop-to-create-a-three-expression-loop\"><strong>How to use a Bash for loop to create a three-expression loop<\/strong><\/h3><p>Using a Bash for loop to create a three-expression loop uses a structure similar to the C programming language. This structure is comprised of three writing expressions &ndash; an initializer (<strong>EXP1<\/strong>), a condition (<strong>EXP2<\/strong>), and a counting step (<strong>EXP3<\/strong>).<\/p><p>The initializer sets the initial script variable, and the condition determines whether or not the loop continues. Meanwhile, the counting step alters the initial value until it meets the specified condition. The syntax of this loop is as follows:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for (( EXP1; EXP2; EXP3 ))\ndo\n    command1\n    command2\n    command3\ndone<\/pre><p>For a better understanding, consider the following code example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\nfor (( c=1; c&lt;=5; c++ ))\ndo  \n  echo \"The number $c\"\ndone<\/pre><p>The code sets the loop&rsquo;s initial value as <strong>1<\/strong>. The loop will run as long as the condition in <strong>EXP2<\/strong> is true &ndash; the code variable shouldn&rsquo;t be bigger than <strong>5<\/strong>. The counting expression has the <strong>++<\/strong> sign, which increments the initial value by one each time the loop runs.<\/p><p>The bash script will echo a message &ldquo;<strong>$c<\/strong>&rdquo; which refers to the loop value, starting from <strong>1 <\/strong>until it reaches the specified condition. The output will be as follows:<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/02\/three-expression-bash-loop-output.png\"><img decoding=\"async\" width=\"831\" height=\"132\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/three-expression-bash-loop-output.png\/public\" alt=\"The three-expression bash for loop output\" class=\"wp-image-94144\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/three-expression-bash-loop-output.png\/w=831,fit=scale-down 831w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/three-expression-bash-loop-output.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/three-expression-bash-loop-output.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/three-expression-bash-loop-output.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 831px) 100vw, 831px\" \/><\/a><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-how-to-use-a-bash-for-loop-to-create-an-infinite-loop\"><strong>How to use a Bash for loop to create an infinite loop<\/strong><\/h3><p>Creating an infinity loop using a Bash for loop lets you execute code indefinitely until you terminate the process manually by pressing <strong>Ctrl + C<\/strong>.<strong> <\/strong>There are different ways to do so, such as using the <strong>while <\/strong>expression:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">while true \ndo \n   echo \"Hello, world!\"\n   sleep 1\ndone <\/pre><p>When the condition is <strong>true<\/strong>, the command will print the <strong>Hello, world!<\/strong> message with a one-second delay. The snippet uses the <strong>while true <\/strong>conditional statement to enable the code to always return the successful exit status.<\/p><p>Since the condition remains <strong>true<\/strong>, the code will keep looping the <a href=\"\/tutorials\/echo-command-linux\"><strong>echo <\/strong>command<\/a> to print the message. Another method is to use the three-expression infinite loop:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for (( ; ; ))\ndo\n   echo \"Hello, world!\"\n   sleep 1\ndone<\/pre><p>In the snippet, we set all the expressions to empty. Since there&rsquo;s no termination condition to meet, the loop will continue until the user stops it.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/02\/infinite-bash-loop-output.png\"><img decoding=\"async\" width=\"653\" height=\"88\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/infinite-bash-loop-output.png\/public\" alt=\"The infinite bash for loop output\" class=\"wp-image-94145\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/infinite-bash-loop-output.png\/w=653,fit=scale-down 653w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/infinite-bash-loop-output.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/infinite-bash-loop-output.png\/w=150,fit=scale-down 150w\" sizes=\"(max-width: 653px) 100vw, 653px\" \/><\/a><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-how-to-use-a-bash-for-loop-to-create-the-skip-and-continue-loop\"><strong>How to use a Bash for loop to create the skip and continue loop<\/strong><\/h3><p>A Bash for loop lets you create a loop that skips a specific value and continues running afterward. This operation uses the following syntax:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for i in 1 2 3 4 5\ndo\n   if [condition]\n   then\n      #Continue with the next iteration of i and skip the statement\n      continue   \n   fi\n      statement\ndone<\/pre><p>Here&rsquo;s a skip-and-continue loop code example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for i in {1..5}\ndo\n   if [[ \"$i\" == '4' ]]\n   then\n      continue   \n   fi\n      echo \"Hello $i\"\ndone<\/pre><p>In the snippet, we define the items to modify as numbers one to five. We add an <strong>if<\/strong> condition, stating that when the variable value equals <strong>4<\/strong>, the loop doesn&rsquo;t run the code and continues to the next value. It means the loop will operate on <strong>1<\/strong>, <strong>2<\/strong>, <strong>3<\/strong>, and <strong>5<\/strong>, as the output shows:<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/02\/skip-continue-bash-loop-output.png\"><img decoding=\"async\" width=\"727\" height=\"106\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/skip-continue-bash-loop-output.png\/public\" alt=\"The skip-continue bash loop output\" class=\"wp-image-94146\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/skip-continue-bash-loop-output.png\/w=727,fit=scale-down 727w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/skip-continue-bash-loop-output.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/skip-continue-bash-loop-output.png\/w=150,fit=scale-down 150w\" sizes=\"(max-width: 727px) 100vw, 727px\" \/><\/a><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-how-to-use-a-bash-for-loop-to-create-a-conditional-exit-with-break-loop\"><strong>How to use a Bash for loop to create a conditional exit with break loop<\/strong><\/h3><p>A Bash for loop lets you create a loop that automatically stops when it meets a specific condition. using the <strong>for-in<\/strong> construct like this syntax:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for i in 1 2 3 4 5\n   do\n   if [condition]\n   then\n      break\n   fi\n   statement\ndone\n<\/pre><p>You can add another command at the end of the code, which will run after the loop ends. Consider the following example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for state in Alabama Alaska Arizona Arkansas California\ndo\n   if [[ \"$state\" == 'Arkansas' ]]; then\n      break\n   fi\n   echo \"state: $state\"\ndone\n\necho 'That&rsquo;s all!'<\/pre><p>The loop script prints all state names specified in the list but stops once the break condition is met, namely when the current value equals <strong>Arkansas<\/strong>. Then, it moves to the next instruction to echo the <strong>That&rsquo;s all!<\/strong> message. Here&rsquo;s what the output looks like:<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-loop-break-condition-output.png\"><img decoding=\"async\" width=\"662\" height=\"110\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-loop-break-condition-output.png\/public\" alt=\"The output of bash loop with break conditional exit\" class=\"wp-image-94147\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-loop-break-condition-output.png\/w=662,fit=scale-down 662w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-loop-break-condition-output.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/bash-loop-break-condition-output.png\/w=150,fit=scale-down 150w\" sizes=\"(max-width: 662px) 100vw, 662px\" \/><\/a><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-how-to-effectively-use-a-bash-script-in-hostinger-vps\"><strong>How to effectively use a Bash script in Hostinger VPS<\/strong><\/h3><p>Using a Bash for loop with your web hosting provider&rsquo;s features can further simplify your server administration tasks, making it more efficient. <\/p><p>For example, Hostinger VPS plans have a <strong>browser terminal<\/strong> built into our hosting custom control panel, hPanel. It lets you run Linux commands and utilities like bash loop directly from your web browser.<\/p><figure class=\"wp-block-image size-large\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-vps-hosting\" href=\"\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/public\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><p>We have an AI agent, <strong>Kodee<\/strong>, that simplifies VPS management for beginners. For example, you can ask it to generate Bash for loop scripts for various tasks using simple prompts like &ldquo;Can you generate a Bash for loop that restarts a list of services?&rdquo; or &ldquo;Please create a Bash for loop to back up multiple directories.&rdquo;<\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e05e8124569\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img decoding=\"async\" width=\"386\" height=\"442\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/kodee-generates-a-bash-for-loop-script.png\/public\" alt=\"Kodee generates a bash for loop script based on a user request\" class=\"wp-image-131829\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/kodee-generates-a-bash-for-loop-script.png\/w=386,fit=scale-down 386w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/kodee-generates-a-bash-for-loop-script.png\/w=262,fit=scale-down 262w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/02\/kodee-generates-a-bash-for-loop-script.png\/w=131,fit=scale-down 131w\" sizes=\"(max-width: 386px) 100vw, 386px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>To access the tool, log in to hPanel and click on <strong>VPS<\/strong> in the top menu. Select the applicable server and navigate to <strong>Kodee <\/strong>in the sidebar. To get accurate results, ensure your <a href=\"\/tutorials\/ai-prompts-for-web-developers\">web development AI prompts<\/a> are specific and clear.<\/p><p>Check out our <a href=\"\/tutorials\/ai-prompts-for-vps-management\">AI prompts for VPS management<\/a> guide to learn more about using <strong>Kodee<\/strong> for various tasks.<\/p><p>\n\n\n<div><p class=\"important\"><strong>Important!<\/strong> Due to AI limitations, some answers may be inaccurate or obsolete.<\/p><\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-key-takeaways\">Key takeaways <\/h2><p>A Bash for loop is great for automating repetitive tasks, processing data, and managing a UNIX-like system. For example, with a single execution, you can iterate through names to automatically create new users when <a href=\"\/tutorials\/how-to-set-up-vps\">setting up a new VPS<\/a>. <\/p><p>As explained, you can perform various assignments with a Bash for loop by combining it with other Bash constructs, like <strong>for-in<\/strong>. You can further expand its functionality by incorporating other command-line tools to create an efficient way to complete a task. <\/p><p>Now that you understand the basics of a Bash for loop, it&rsquo;s time to practice using it in day-to-day tasks. If you use Hostinger VPS, asking <strong>Kodee<\/strong> to write or break down Bash for loop scripts makes the learning process more intuitive, helping you familiarize yourself with it more quickly. <\/p><p>For inspiration, consider checking out our <a href=\"\/tutorials\/bash-script-example\">Bash script example tutorial<\/a> to learn more about the real-world application of the programming language for various purposes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A Bash for loop is a statement in the Bash programming language that allows a code or script to run [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/tutorials\/bash-for-loop\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":110,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"What is a bash for loop? Practical examples and syntax","rank_math_description":"A Bash for loop is a statement that lets you run a set of commands repeatedly for each listed item. Explore its syntax and practical examples.","rank_math_focus_keyword":"bash for loop","footnotes":""},"categories":[22648,22644],"tags":[],"class_list":["post-15680","post","type-post","status-publish","format-standard","hentry","category-managing-monitoring-and-security","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/bash-for-loop","default":0},{"locale":"fr-FR","link":"https:\/\/www.hostinger.com\/fr\/tutoriels\/guide-bash-boucle-for","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/bash-for-loop-guia-ejemplos","default":0},{"locale":"id-ID","link":"https:\/\/www.hostinger.com\/id\/tutorial\/bash-for-loop","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/bash-for-loop","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/bash-for-loop","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/bash-for-loop","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/bash-for-loop-guia-ejemplos","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/bash-for-loop-guia-ejemplos","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/bash-for-loop-guia-ejemplos","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/bash-for-loop","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/bash-for-loop","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/bash-for-loop","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/bash-for-loop","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/15680","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/users\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/comments?post=15680"}],"version-history":[{"count":35,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/15680\/revisions"}],"predecessor-version":[{"id":138369,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/15680\/revisions\/138369"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/media?parent=15680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/categories?post=15680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/tags?post=15680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}