Cloud House Technologies Logo
CloudHouse Technologies
HomeServicesProjectsBlogAbout UsCareersContact UsLogin
    Cloud House Technologies Logo
    CloudHouse Technologies
    HomeServicesProjectsBlogAbout UsCareersContact UsLogin

    Introduction to Bash For Loops: A Beginner’s Guide

    sreekutty

    Devops

    Last Updated: 22 June 2026
    Introduction to Bash For Loops: A Beginner’s Guide
    🖥️12,400+PCs Fixed
    ⭐4.9★Google Rating
    ⚡<15 minAvg. Response
    🛡️ISO 27001Certified

    Table of Contents

    Why Are ‘For’ Loops Necessary? Basic Syntax of Bash ‘For’ Loop Using Bash ‘For’ Loop Looping through Files and Directories Iterating over a Range of Numbers Using ‘for’ with Command Substitution Nested ‘for’ Loops

    1. Why Are ‘For’ Loops Necessary?

    Without loops, performing an action on 100 different files would require you to type 100 separate commands. Loops are necessary because they:

    Eliminate human error : Manually typing filenames often leads to typos.

    Save Time : You write the logic once, and the computer handles the volume.

    Enable Scalability: A loop that works for 5 files will work exactly the same way for 5,000 files.

    2. Basic Syntax of Bash ‘For’ Loop

    The structure of a Bash loop follows a strict "Header, Do, and Done" format.

    for variable in list
    do
    do
        command1
        command2
    done
    

    variable: A temporary name (like item or f) that represents the current object being processed.

    list: The collection of items (strings, numbers, or files) the loop will cycle through.

    do / done: These keywords act as brackets, containing all the code that should be repeated.

    3. Using Bash ‘For’ Loop

    In its simplest form, you can provide a manual list of strings. The loop will assign each string to the variable one by one.

    for color in red green blue
    do
        echo "The current color is $color"
    done
    

    4. Looping through Files and Directories

    This is the most common use case for system administrators. By using wildcards (like *), you can tell Bash to find every file that matches a pattern.

    Bash

    #Example: Rename all .txt files to .bak
    for file in *.txt
    do
        mv "$file" "${file%.txt}.bak"
    done
    

    Note: Always use double quotes "$file" to handle filenames that contain spaces.

    5. Iterating over a Range of Numbers

    If you need to perform an action a specific number of times, you can use curly brace expansion {start..end}.

    Bash

    #Create folders named folder1 through folder5
    for i in {1..5}
    do
        mkdir "folder$i"
    done
    

    6. Using ‘for’ with Command Substitution

    You can use the output of another command as the list for your loop. This is done using the $(command) syntax.

    Bash

    #Loop through all users listed in the system
    for user in $(cut -d: -f1 /etc/passwd)
    do
        echo "Found system user: $user"
    done
    

    7. Nested ‘for’ Loops

    A nested loop is a loop inside another loop. The "inner" loop will complete its entire cycle for every single step of the "outer" loop. This is useful for creating grids or processing multi-layered data.

    Bash

    
    for letter in A B
    do
        for number in 1 2
        do
       echo "Combination: $letter$number"
        done
    done
    

    Output:

    Combination: A1

    Combination: A2

    Combination: B1

    Combination: B2

    terminalcommandsbash scripting

    Get the Free Linux Server Admin Cheatsheet (PDF)

    Essential commands for server management, networking, and troubleshooting — all on one printable page.

    Running Linux servers? Let us manage them for you.

    Our Managed Linux Server plans cover updates, security hardening, monitoring, and 24/7 incident response — so your servers stay up and your team stays focused.

    • Proactive OS patching and security updates
    • 24×7 monitoring with instant alerting
    • Backup configuration and disaster recovery
    • Dedicated Linux engineers on call
    See Pricing Plans →

    What our customers say

    “Our production server went down at 2 AM. CloudHouse had it back online in under 20 minutes. Incredible response time.”

    Arun S.

    CTO, SaaS Startup

    “They migrated our entire infrastructure from Ubuntu 18 to 22 with zero downtime. Couldn't have asked for better.”

    Deepak N.

    DevOps Lead

    Frequently Asked Questions

    Yes — 90% of fixes are done over a secure remote session in 15–30 minutes. Our technicians connect safely using industry-standard tools.

    Book your free 15-minute diagnosis

    A certified technician will call you back within 15 minutes during business hours.

    Share this article

    Leave a Comment

    Comments (0)

    Loading comments...

    Still stuck?

    Free remote diagnosis by a certified engineer. 15 minutes. No credit card.

    Call Now — FreeWhatsApp Us

    Why CloudHouse?

    • ISO 27001:2022 certified
    • 12,400+ devices supported
    • 4.9★ on Google
    • Sub-15-minute response

    CloudHouse Technologies

    Innovative cloud solutions for modern businesses. We deliver cutting-edge technology with exceptional service.

    Contact Us

    CloudHouse Technologies Pvt.Ltd
    Special Economic Zone(SEZ),
    Infopark Thirissur,4B-15,
    Indeevaram,Nalukettu Road,
    Koratty, Kerala, India-680308
    0480-27327360
    info@cloudhousetechnologies.com

    Quick Links

    • Our Services
    • Gold Loan Software
    • About Us
    • Contact
    • Terms and Conditions
    • Privacy Policy
    ISO27001:2022
    Certified

    © 2026 CloudHouse Technologies Pvt.Ltd. All rights reserved.

    Back to top