The Tech Edvocate

Top Menu

  • Advertisement
  • Apps
  • Home Page
  • Home Page Five (No Sidebar)
  • Home Page Four
  • Home Page Three
  • Home Page Two
  • Home Tech2
  • Icons [No Sidebar]
  • Left Sidbear Page
  • Lynch Educational Consulting
  • My Account
  • My Speaking Page
  • Newsletter Sign Up Confirmation
  • Newsletter Unsubscription
  • Our Brands
  • Page Example
  • Privacy Policy
  • Protected Content
  • Register
  • Request a Product Review
  • Shop
  • Shortcodes Examples
  • Signup
  • Start Here
    • Governance
    • Careers
    • Contact Us
  • Terms and Conditions
  • The Edvocate
  • The Tech Edvocate Product Guide
  • Topics
  • Write For Us
  • Advertise

Main Menu

  • Start Here
    • Our Brands
    • Governance
      • Lynch Educational Consulting, LLC.
      • Dr. Lynch’s Personal Website
      • Careers
    • Write For Us
    • The Tech Edvocate Product Guide
    • Contact Us
    • Books
    • Edupedia
    • Post a Job
    • The Edvocate Podcast
    • Terms and Conditions
    • Privacy Policy
  • Topics
    • Assistive Technology
    • Child Development Tech
    • Early Childhood & K-12 EdTech
    • EdTech Futures
    • EdTech News
    • EdTech Policy & Reform
    • EdTech Startups & Businesses
    • Higher Education EdTech
    • Online Learning & eLearning
    • Parent & Family Tech
    • Personalized Learning
    • Product Reviews
  • Advertise
  • Tech Edvocate Awards
  • The Edvocate
  • Pedagogue
  • School Ratings

logo

The Tech Edvocate

  • Start Here
    • Our Brands
    • Governance
      • Lynch Educational Consulting, LLC.
      • Dr. Lynch’s Personal Website
        • My Speaking Page
      • Careers
    • Write For Us
    • The Tech Edvocate Product Guide
    • Contact Us
    • Books
    • Edupedia
    • Post a Job
    • The Edvocate Podcast
    • Terms and Conditions
    • Privacy Policy
  • Topics
    • Assistive Technology
    • Child Development Tech
    • Early Childhood & K-12 EdTech
    • EdTech Futures
    • EdTech News
    • EdTech Policy & Reform
    • EdTech Startups & Businesses
    • Higher Education EdTech
    • Online Learning & eLearning
    • Parent & Family Tech
    • Personalized Learning
    • Product Reviews
  • Advertise
  • Tech Edvocate Awards
  • The Edvocate
  • Pedagogue
  • School Ratings
  • 2.3 Million Ricky Joy Sour Crush Candies Recalled: This One Detail Is a Parent’s Nightmare

  • The Staggering Startup Valuation Reset: Why Unicorns Are Vanishing

  • The Glaring Contradiction: Why Big Tech Is Shedding 128,536 Jobs While Investing Billions in AI

  • Insomniac’s Bold Move: The Truth Behind Marvel’s Wolverine Sabretooth ‘Romance’ That Stunned Fans

  • Explosive: ESIC Ban Juan Angulo For Life — The Unseen Crisis Gripping Dota 2

  • This Crucial New Bill Could Finally Fix Student Loan Forgiveness for Millions

  • Massive School Bans on AI: Are We Sacrificing Progress for Protection?

  • Autonomous AI Breaches: Anthropic Confirms Four Attacks and Weaponized Bots

  • Jaw-Dropping Footage: This Waymo Collision Has Everyone Talking

  • Hyundai’s Game-Changing EV Battery Technology: Why It Matters For Your Next Car

Digital & Mobile Technology
Home›Digital & Mobile Technology›Everything You Need to Know About Bash For Loops in Linux

Everything You Need to Know About Bash For Loops in Linux

By Matthew Lynch
June 13, 2023
0
Spread the love

As a Linux user, you might often come across scenarios where you need to iterate through a set of items or a list of files. In such cases, bash for loops can come in handy. In this article, we’ll cover everything you need to know about bash for loops in Linux.

What is a bash for loop?

In a bash for loop, you essentially iterate through a set of items, perform some kind of action on each item, and then move on to the next item. The loop continues until you’ve iterated through the entire set of items.

The basic syntax of a bash for loop is as follows:

“`
for ITEM in SET_OF_ITEMS
do
# Action to be performed on each ITEM
done
“`

Here, `ITEM` is a variable that represents each item in the set, and `SET_OF_ITEMS` is the set of items that you want to iterate through. You can perform any kind of action on each item within the loop, such as reading a file or processing data.

Example of a simple for loop

Let’s look at a simple example of a bash for loop where we print the numbers 1 to 5:

“`
#!/bin/bash

for i in 1 2 3 4 5
do
echo $i
done
“`

In this example, `i` represents each number in the set from 1 to 5. The loop iterates through the set and prints each number using the `echo` command.

Using variables in a for loop

You can also use variables in a bash for loop. For example, let’s say you want to iterate through a set of file names stored in a variable:

“`
#!/bin/bash

FILES=”file1.txt file2.txt file3.txt”

for f in $FILES
do
echo Processing $f
# Perform some action on $f
done
“`

In this example, `FILES` is a variable that contains a set of file names separated by spaces. The loop iterates through the set and performs some action on each file.

Using wildcards in a for loop

You can also use wildcards to iterate through a set of files that match a specific pattern. For example, if you want to iterate through all files with the .txt extension in a directory:

“`
#!/bin/bash

for f in *.txt
do
echo Processing $f
# Perform some action on $f
done
“`

In this example, `*.txt` is a wildcard pattern that matches all files in the current directory with the .txt extension. The loop iterates through the set and performs some action on each file.

Using counter variables in a for loop

You can also use a counter variable to track the number of iterations in a for loop. For example, let’s say you want to iterate through a set of numbers and print out each number along with its position in the set:

“`
#!/bin/bash

NUMBERS=”1 2 3 4 5″

count=1
for n in $NUMBERS
do
echo $count: $n
((count++))
done
“`

In this example, `count` is a variable that tracks the number of iterations in the loop. The loop iterates through the set and prints out each number along with its position in the set.

Conclusion

Bash for loops are a powerful tool for iterating through sets of items in Linux. In this article, we covered the basic syntax of a bash for loop, how to use variables and wildcards in a for loop, and how to use counter variables to track the number of iterations. With this knowledge, you should be able to use bash for loops to perform a wide range of tasks in Linux.

Previous Article

How to Use the Free Gmail SMTP ...

Next Article

How to Shoot Shadow Photography: Essential Tips

Matthew Lynch

Related articles More from author

  • Digital & Mobile Technology

    How to Set Up and Use SSH in Linux

    May 8, 2023
    By Matthew Lynch
  • Digital & Mobile Technology

    How to Create New Folders to Organize Mail in Outlook

    June 2, 2023
    By Matthew Lynch
  • Digital & Mobile Technology

    How to Set Up a New Gmail Account for Yourself or Others

    June 7, 2023
    By Matthew Lynch
  • Digital & Mobile Technology

    The 10 Most Popular Free Apps Everyone Needs on Their iPhone

    June 6, 2023
    By Matthew Lynch
  • Digital & Mobile Technology

    How to Get Local Weather Channels on DirecTV

    April 26, 2023
    By Matthew Lynch
  • Digital & Mobile Technology

    The Top 8 Wii Puzzle Games

    July 28, 2023
    By Matthew Lynch

Search

Login & Registration

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Newsletter

Signup for The Tech Edvocate Newsletter and have the latest in EdTech news and opinion delivered to your email address!

About Us

Since technology is not going anywhere and does more good than harm, adapting is the best course of action. That is where The Tech Edvocate comes in. We plan to cover the PreK-12 and Higher Education EdTech sectors and provide our readers with the latest news and opinion on the subject. From time to time, I will invite other voices to weigh in on important issues in EdTech. We hope to provide a well-rounded, multi-faceted look at the past, present, the future of EdTech in the US and internationally.

We started this journey back in June 2016, and we plan to continue it for many more years to come. I hope that you will join us in this discussion of the past, present and future of EdTech and lend your own insight to the issues that are discussed.

Newsletter

Signup for The Tech Edvocate Newsletter and have the latest in EdTech news and opinion delivered to your email address!

Contact Us

The Tech Edvocate
910 Goddin Street
Richmond, VA 23231
(601) 630-5238
[email protected]

Copyright © 2026 Matthew Lynch. All rights reserved.