<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bash for loops | ServerAvatar</title>
	<atom:link href="https://serveravatar.com/tag/bash-for-loops/feed/" rel="self" type="application/rss+xml" />
	<link>https://serveravatar.com</link>
	<description>The first, Fully Hybrid Cloud Hosting Solution.</description>
	<lastBuildDate>Wed, 24 Sep 2025 06:40:03 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://serveravatar.com/wp-content/uploads/2025/02/cropped-favicon-32x32.png</url>
	<title>Bash for loops | ServerAvatar</title>
	<link>https://serveravatar.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Introduction to Bash For Loops: Beginner&#8217;s Guide</title>
		<link>https://serveravatar.com/introduction-to-bash-for-loops/</link>
					<comments>https://serveravatar.com/introduction-to-bash-for-loops/#respond</comments>
		
		<dc:creator><![CDATA[Meghna Meghwani]]></dc:creator>
		<pubDate>Thu, 17 Jul 2025 18:35:00 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Guide]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[Automate tasks with Bash]]></category>
		<category><![CDATA[Bash for loops]]></category>
		<category><![CDATA[Command line automation]]></category>
		<category><![CDATA[PHP Hosting]]></category>
		<guid isPermaLink="false">https://serveravatar.com/?p=26197</guid>

					<description><![CDATA[<p>If you&#8217;ve ever wished your computer could take over your repetitive tasks, like checking files, updating reports, or processing logs, then&#160;Bash for loops&#160;are about to become your new best friend. We can say that Bash is the language that you can use to talk with your computer. And ever wondering, what is&#160;for loops?&#160;They’re like your [&#8230;]</p>
<p>The post <a href="https://serveravatar.com/introduction-to-bash-for-loops/">Introduction to Bash For Loops: Beginner’s Guide</a> first appeared on <a href="https://serveravatar.com">ServerAvatar</a>.</p>]]></description>
										<content:encoded><![CDATA[<figure class="wp-block-image size-large"><a href="https://serveravatar.com?ref=blog" target="_blank" rel=" noreferrer noopener"><img fetchpriority="high" decoding="async" width="1024" height="127" src="https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-1024x127.png" alt="Blog banner - ServerAvatar" class="wp-image-28182" srcset="https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-1024x127.png 1024w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-300x37.png 300w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-768x95.png 768w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-1536x190.png 1536w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-2048x253.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>



<p>If you&#8217;ve ever wished your computer could take over your repetitive tasks, like checking files, updating reports, or processing logs, then&nbsp;<strong>Bash for loops</strong>&nbsp;are about to become your new best friend.</p>



<p>We can say that Bash is the language that you can use to talk with your computer. And ever wondering, what is&nbsp;<strong>for loops?</strong>&nbsp;They’re like your loyal assistants who will repeat tasks as many times as you want, without getting tired.</p>



<p>But don’t worry if you’re not a programmer or a technical expert. We’re going to break everything down in simple terms so that&nbsp;<em>anyone</em>, even with zero scripting experience, can understand.</p>



<p>Ready to dive into automation and shell scripting? Let’s get started!</p>



<h2 class="wp-block-heading">What is Bash and Why Use It?</h2>



<p><strong>Bash</strong>&nbsp;(short form of&nbsp;<strong>Bourne Again SHell</strong>) is a command-line interface used in Linux and macOS systems. You can use it to run commands, manipulate files, and automate tasks.</p>



<p>It’s like telling your computer what to do using text. And why should you care? Because Bash can save you&nbsp;<strong>hours</strong>&nbsp;of manual work with just a few lines of code.</p>



<h2 class="wp-block-heading">What is a Loop in Programming?</h2>



<p>Imagine you&#8217;re folding laundry. Each item goes through the same process, pick it up, fold it, and stack it. A&nbsp;<strong>loop</strong>&nbsp;in programming does the same thing: it repeats a set of actions until a condition is met.</p>



<p>Loops are essential for automation. They help you handle tasks like:</p>



<ul class="wp-block-list">
<li>Repeating commands</li>



<li>Processing multiple files</li>



<li>Running tests on different inputs</li>
</ul>



<h2 class="wp-block-heading">Introduction to Bash For Loops</h2>



<p><strong>For loops</strong>&nbsp;are most commonly used type of loop in Bash. They will repeat a task for the each item that included in a list.</p>



<p>For Example</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>for item in apple banana cherry
do
  echo $item
done</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #C586C0">for</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">item</span><span style="color: #D4D4D4"> </span><span style="color: #C586C0">in</span><span style="color: #D4D4D4"> apple banana cherry</span></span>
<span class="line"><span style="color: #C586C0">do</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #DCDCAA">echo</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">$item</span></span>
<span class="line"><span style="color: #C586C0">done</span></span></code></pre></div>



<p>The for loop in above example will print each fruit name on new line.</p>



<p><strong>Output:</strong><br><a href="https://storage.3.basecamp.com/5840738/blobs/41a95cfc-62f3-11f0-a6b4-0242ac120005/download/image.png" target="_blank" rel="noreferrer noopener"></a></p>



<figure class="wp-block-image size-full"><img decoding="async" width="800" height="189" src="https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T162919.934.png" alt="Bash for loops" class="wp-image-26198" srcset="https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T162919.934.png 800w, https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T162919.934-300x71.png 300w, https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T162919.934-768x181.png 768w" sizes="(max-width: 800px) 100vw, 800px" /></figure>



<h2 class="wp-block-heading">Basic Syntax of Bash For Loops</h2>



<p>Here’s what the structure of a Bash for loop looks like:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>for variable in list
do
  command(s)
done</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #C586C0">for</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">variable</span><span style="color: #D4D4D4"> </span><span style="color: #C586C0">in</span><span style="color: #D4D4D4"> list</span></span>
<span class="line"><span style="color: #C586C0">do</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #DCDCAA">command</span><span style="color: #D4D4D4">(s)</span></span>
<span class="line"><span style="color: #C586C0">done</span></span></code></pre></div>



<ul class="wp-block-list">
<li>variable is a placeholder.</li>



<li>list is a set of values.</li>



<li>do starts the block of commands.</li>



<li>done ends the block.</li>
</ul>



<p>It’s that simple!</p>



<h2 class="wp-block-heading">Example of Bash For Loop: Hello, World!</h2>



<p>Let’s write a for loop that prints “Hello, World!” five times.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>for i in {1..5}
do
  echo "Hello, World!"
done</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #C586C0">for</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">i</span><span style="color: #D4D4D4"> </span><span style="color: #C586C0">in</span><span style="color: #D4D4D4"> {</span><span style="color: #DCDCAA">1..5}</span></span>
<span class="line"><span style="color: #C586C0">do</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #DCDCAA">echo</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&quot;Hello, World!&quot;</span></span>
<span class="line"><span style="color: #C586C0">done</span></span></code></pre></div>



<p><strong>Output:<br></strong><a href="https://storage.3.basecamp.com/5840738/blobs/63d8d60e-62f3-11f0-bec7-0242ac120004/download/image.png" target="_blank" rel="noreferrer noopener"></a></p>



<figure class="wp-block-image size-full"><img decoding="async" width="815" height="231" src="https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163106.106.png" alt="Print Hello World" class="wp-image-26199" srcset="https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163106.106.png 815w, https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163106.106-300x85.png 300w, https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163106.106-768x218.png 768w" sizes="(max-width: 815px) 100vw, 815px" /></figure>



<h2 class="wp-block-heading">What’s happening here?</h2>



<ul class="wp-block-list">
<li>&#8220;{1..5}&#8221; will run the loop from 1 to 5.</li>



<li>&#8220;i&#8221; takes each value from 1 to 5.</li>



<li>echo prints the message.</li>
</ul>



<h2 class="wp-block-heading">How Bash For Loops Work</h2>



<p>Behind the scenes, Bash loops:</p>



<ol class="wp-block-list">
<li>It will assign a value from list to loop variable to execute.</li>



<li>Execute the code block.</li>



<li>Repeat until all values are used.</li>
</ol>



<p>Think of it as like passing buckets down a line, each person (the loop variable) gets a new bucket (value) and performs the same action.</p>



<h2 class="wp-block-heading">Common Use Cases of Bash For Loops</h2>



<p>You can use Bash for loops for tasks like:</p>



<ul class="wp-block-list">
<li>Renaming or moving files</li>



<li>Automating backups</li>



<li>Running scripts on multiple servers</li>



<li>Reading lines from a file</li>



<li>Monitoring system logs</li>
</ul>



<p>Once you start using them, you’ll wonder how useful it is!</p>



<figure class="wp-block-image size-large"><a href="https://serveravatar.com?ref=blog" target="_blank" rel=" noreferrer noopener"><img loading="lazy" decoding="async" width="1024" height="127" src="https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-1024x127.png" alt="Blog banner - ServerAvatar" class="wp-image-28184" srcset="https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-1024x127.png 1024w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-300x37.png 300w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-768x95.png 768w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-1536x190.png 1536w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-2048x254.png 2048w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure>



<h2 class="wp-block-heading">Bash For Loop with Numeric Ranges</h2>



<p>Want to count from 1 to 10?</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>for i in {1..10}
do
  echo $i
done</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #C586C0">for</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">i</span><span style="color: #D4D4D4"> </span><span style="color: #C586C0">in</span><span style="color: #D4D4D4"> {</span><span style="color: #DCDCAA">1..10}</span></span>
<span class="line"><span style="color: #C586C0">do</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #DCDCAA">echo</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">$i</span></span>
<span class="line"><span style="color: #C586C0">done</span></span></code></pre></div>



<p><strong>Output:<br></strong><a href="https://storage.3.basecamp.com/5840738/blobs/7c804872-62f3-11f0-83d1-0242ac120004/download/image.png" target="_blank" rel="noreferrer noopener"></a></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="808" height="337" src="https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163444.408.png" alt="Bash For Loop With Numeric" class="wp-image-26200" srcset="https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163444.408.png 808w, https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163444.408-300x125.png 300w, https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163444.408-768x320.png 768w" sizes="auto, (max-width: 808px) 100vw, 808px" /></figure>



<p>Or with step values:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>for i in {1..10..2}
do
  echo $i
done</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #C586C0">for</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">i</span><span style="color: #D4D4D4"> </span><span style="color: #C586C0">in</span><span style="color: #D4D4D4"> {</span><span style="color: #DCDCAA">1..10..2}</span></span>
<span class="line"><span style="color: #C586C0">do</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #DCDCAA">echo</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">$i</span></span>
<span class="line"><span style="color: #C586C0">done</span></span></code></pre></div>



<p>In the output, it will print 1, 3, 5, 7, 9 as you can see in the image below.</p>



<p><strong>Output:</strong><br><a href="https://storage.3.basecamp.com/5840738/blobs/9dba0514-62f3-11f0-8113-0242ac120004/download/image.png" target="_blank" rel="noreferrer noopener"></a></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="811" height="235" src="https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163537.892.png" alt="Bash with Step Value" class="wp-image-26201" srcset="https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163537.892.png 811w, https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163537.892-300x87.png 300w, https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163537.892-768x223.png 768w" sizes="auto, (max-width: 811px) 100vw, 811px" /></figure>



<h2 class="wp-block-heading">Bash For Loop with Lists</h2>



<p>You can manually define a list too:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>for animal in cat dog cow
do
  echo "The animal is: $animal"
done</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #C586C0">for</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">animal</span><span style="color: #D4D4D4"> </span><span style="color: #C586C0">in</span><span style="color: #D4D4D4"> cat dog cow</span></span>
<span class="line"><span style="color: #C586C0">do</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #DCDCAA">echo</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&quot;The animal is: </span><span style="color: #9CDCFE">$animal</span><span style="color: #CE9178">&quot;</span></span>
<span class="line"><span style="color: #C586C0">done</span></span></code></pre></div>



<p><strong>Output:<br></strong><a href="https://storage.3.basecamp.com/5840738/blobs/b38a9eee-62f3-11f0-b70d-0242ac120004/download/image.png" target="_blank" rel="noreferrer noopener"></a></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="799" height="188" src="https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163631.224.png" alt="Bash Loop With List" class="wp-image-26202" srcset="https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163631.224.png 799w, https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163631.224-300x71.png 300w, https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163631.224-768x181.png 768w" sizes="auto, (max-width: 799px) 100vw, 799px" /></figure>



<p><strong>Or use variables:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>mylist="red blue green"
for color in $mylist
do
  echo $color
done</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #9CDCFE">mylist</span><span style="color: #D4D4D4">=</span><span style="color: #CE9178">&quot;red blue green&quot;</span></span>
<span class="line"><span style="color: #C586C0">for</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">color</span><span style="color: #D4D4D4"> </span><span style="color: #C586C0">in</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">$mylist</span></span>
<span class="line"><span style="color: #C586C0">do</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #DCDCAA">echo</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">$color</span></span>
<span class="line"><span style="color: #C586C0">done</span></span></code></pre></div>



<p><strong>Output:<br></strong><a href="https://storage.3.basecamp.com/5840738/blobs/c4e60110-62f3-11f0-b44f-0242ac120004/download/image.png" target="_blank" rel="noreferrer noopener"></a></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="806" height="208" src="https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163848.087.png" alt="Bash Loop For Variable" class="wp-image-26203" srcset="https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163848.087.png 806w, https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163848.087-300x77.png 300w, https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163848.087-768x198.png 768w" sizes="auto, (max-width: 806px) 100vw, 806px" /></figure>



<h2 class="wp-block-heading">Nested For Loops</h2>



<p>Want to loop inside another loop? That’s called&nbsp;<strong>nested loop</strong>.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>for i in 1 2 3
do
  for j in A B
  do
    echo "$i$j"
  done
done</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #C586C0">for</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">i</span><span style="color: #D4D4D4"> </span><span style="color: #C586C0">in</span><span style="color: #D4D4D4"> 1 2 3</span></span>
<span class="line"><span style="color: #C586C0">do</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #C586C0">for</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">j</span><span style="color: #D4D4D4"> </span><span style="color: #C586C0">in</span><span style="color: #D4D4D4"> A B</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #C586C0">do</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">echo</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&quot;</span><span style="color: #9CDCFE">$i$j</span><span style="color: #CE9178">&quot;</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #C586C0">done</span></span>
<span class="line"><span style="color: #C586C0">done</span></span></code></pre></div>



<p><strong>This prints:</strong><br>1A<br>1B<br>2A<br>2B<br>3A<br>3B</p>



<p><strong>Output:<br></strong><a href="https://storage.3.basecamp.com/5840738/blobs/e6db96ea-62f3-11f0-841b-0242ac120003/download/image.png" target="_blank" rel="noreferrer noopener"></a></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="807" height="319" src="https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163955.913.png" alt="Nested For Loops" class="wp-image-26204" srcset="https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163955.913.png 807w, https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163955.913-300x119.png 300w, https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T163955.913-768x304.png 768w" sizes="auto, (max-width: 807px) 100vw, 807px" /></figure>



<h2 class="wp-block-heading">Control Statements in For Loops</h2>



<p>You can control your loops with:</p>



<ul class="wp-block-list">
<li><strong>break</strong>&nbsp;– exits the loop early</li>



<li><strong>continue</strong>&nbsp;– skips to the next iteration</li>
</ul>



<p>Example:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>for i in {1..5}
do
  if &#91; $i -eq 3 &#93;; then
    continue
  fi
  echo $i
done</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #C586C0">for</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">i</span><span style="color: #D4D4D4"> </span><span style="color: #C586C0">in</span><span style="color: #D4D4D4"> {</span><span style="color: #DCDCAA">1..5}</span></span>
<span class="line"><span style="color: #C586C0">do</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #C586C0">if</span><span style="color: #D4D4D4"> &#91; </span><span style="color: #9CDCFE">$i</span><span style="color: #D4D4D4"> -eq </span><span style="color: #B5CEA8">3</span><span style="color: #D4D4D4"> &#93;; </span><span style="color: #C586C0">then</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">continue</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #C586C0">fi</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #DCDCAA">echo</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">$i</span></span>
<span class="line"><span style="color: #C586C0">done</span></span></code></pre></div>



<p>This skips printing 3.</p>



<p><strong>Output:</strong></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="797" height="275" src="https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T164129.931.png" alt="Command line" class="wp-image-26206" srcset="https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T164129.931.png 797w, https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T164129.931-300x104.png 300w, https://serveravatar.com/wp-content/uploads/2025/07/image-2025-07-17T164129.931-768x265.png 768w" sizes="auto, (max-width: 797px) 100vw, 797px" /></figure>



<h2 class="wp-block-heading">Tips for Writing Clean Bash For Loops</h2>



<ul class="wp-block-list">
<li><strong>Use meaningful variable names</strong>: file, user, line are better than i or x</li>



<li><strong>Comment your code</strong>&nbsp;so you remember what each part does</li>



<li><strong>Test your loops</strong>&nbsp;with small inputs before running them on large data sets</li>
</ul>



<h2 class="wp-block-heading">Common Mistakes and How to Avoid Them</h2>



<p>Here are some beginner pitfalls:</p>



<ul class="wp-block-list">
<li><strong>Forgetting do and done</strong>: Always include them</li>



<li><strong>Using wrong syntax</strong>: you can use {1..10}, but not 1-10</li>



<li><strong>Misusing spaces</strong>: Always leave spaces around in</li>
</ul>



<p>Avoid these and your loops will run like a charm.</p>



<h2 class="wp-block-heading">Conclusion&nbsp;</h2>



<p>Bash for loops are a simple yet powerful tool for anyone who wants to start automating tasks on their computer. Whether you&#8217;re a casual Linux user or a sysadmin, learning how to loop in Bash opens the door to endless possibilities. So go ahead, experiment, break things, and most importantly, automate like a pro!</p>



<p><strong>Feeling overwhelmed by command-line tasks?</strong><br>You’re not alone, and the good news is, you don’t have to go it alone either. If diving into Bash still feels a bit too technical,&nbsp;<strong>ServerAvatar</strong>&nbsp;offers a smarter, smoother way to manage your servers without getting buried in terminal commands.</p>



<p><strong>Here’s what makes ServerAvatar a great companion:</strong></p>



<ul class="wp-block-list">
<li>Easily handle server setups, app deployments, domains, databases, and more, no deep Linux knowledge required.</li>



<li>Prefer working in the terminal? You’ll still have full SSH access and flexibility to script and automate as you like.</li>
</ul>



<p>With ServerAvatar, whether you’re a beginner or a power user, you can manage servers your way, fast, secure, and hassle-free.</p>



<h2 class="wp-block-heading">FAQs</h2>



<div data-schema-only="false" class="wp-block-aioseo-faq"><h3 class="aioseo-faq-block-question"><strong>1. What is the difference between a Bash for loop and a while loop?</strong></h3><div class="aioseo-faq-block-answer">
<p>A&nbsp;<strong>for loop</strong>&nbsp;runs through a predefined list, while a&nbsp;<strong>while loop</strong>&nbsp;continues as long as a condition is true. Both are useful for different situations.</p>
</div></div>



<div data-schema-only="false" class="wp-block-aioseo-faq"><h3 class="aioseo-faq-block-question"><strong>2. Can I use Bash for loops on Windows?</strong></h3><div class="aioseo-faq-block-answer">
<p>Yes, if you use&nbsp;<strong>Git Bash</strong>,&nbsp;<strong>WSL (Windows Subsystem for Linux)</strong>, or tools like&nbsp;<strong>Cygwin</strong>, you can write and run Bash scripts on Windows.</p>
</div></div>



<p><strong>3. How to loop files in a directory?</strong></p>



<pre class="wp-block-preformatted">for file in *.txt<br>do<br>  echo "Found file: $file"<br>done<br><br>This will go through every .txt file in your current directory.</pre>



<div data-schema-only="false" class="wp-block-aioseo-faq"><h3 class="aioseo-faq-block-question"><strong>4. Are Bash for loops case-sensitive?</strong></h3><div class="aioseo-faq-block-answer">
<p>Yes, Bash treats variables and values as&nbsp;<strong>case-sensitive</strong>, so &#8220;Apple&#8221; and &#8220;apple&#8221; are different.</p>
</div></div>



<div data-schema-only="false" class="wp-block-aioseo-faq"><h3 class="aioseo-faq-block-question"><strong>5. Can I write multi-line commands inside a Bash for loop?</strong></h3><div class="aioseo-faq-block-answer">
<p>Absolutely! You can add as many commands as needed inside the do block. Just make sure each line is indented for clarity.</p>
</div></div>



<div style="font-family: 'Public Sans', sans-serif; background: #fff; border: 1px solid #e1eaff; padding: 40px 35px; border-radius: 16px; margin: 60px 0; box-shadow: 0 10px 32px rgba(0, 0, 0, 0.05); max-width: 860px; margin-left: auto; margin-right: auto; text-align: center;">

  <h2 style="font-size: 28px; font-weight: 800; color: #0b2b53; margin-top: 0; margin-bottom: 16px;">
    Stop Wasting Time on Servers. Start Building Instead.
  </h2>

  <p style="font-size: 17px; color: #333; font-weight: 500; margin: 0 0 12px;">
    You didn’t start your project to babysit servers.
    Let <strong>ServerAvatar</strong> handle deployment, monitoring, and backups —
    so you can focus on growth.
  </p>

  <p style="font-size: 16px; color: #555; font-weight: 400; margin-bottom: 26px;">
    Deploy <strong>WordPress, Laravel, N8N</strong>, and more in minutes.
    No DevOps required. No command line. No stress.
  </p>

  <a href="https://app.serveravatar.com" target="_blank" rel="noopener" style="display: inline-block; background: linear-gradient(135deg, #007bff, #0056d2); color: #fff; text-decoration: none; padding: 16px 30px; font-size: 17px; font-weight: 700; border-radius: 10px; transition: background 0.3s ease;">
    🚀 Try ServerAvatar Free – No Credit Card Needed
  </a>

  <p style="font-size: 14px; color: #888; margin-top: 18px;">
    Trusted by 10,000+ developers and growing.
  </p>
</div><p>The post <a href="https://serveravatar.com/introduction-to-bash-for-loops/">Introduction to Bash For Loops: Beginner’s Guide</a> first appeared on <a href="https://serveravatar.com">ServerAvatar</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://serveravatar.com/introduction-to-bash-for-loops/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
