Shell Scripting / Loops in Shell Scripting

Using for Loops to Iterate Over Lists

In this tutorial, we will explore how to use 'for' loops to iterate over lists in shell scripting. This is a powerful tool for managing and manipulating lists of data.

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Covers looping constructs in shell scripts, including for, while, and until loops.

Using for Loops to Iterate Over Lists in Shell Scripting

Introduction

In this tutorial, we will explore the functionality of 'for' loops, particularly in the context of iterating over lists in shell scripting. This is an essential tool for managing and manipulating data lists.

What you will learn:

  • Understanding of 'for' loops
  • How to use 'for' loops to iterate over lists in shell scripting
  • Best practices when working with 'for' loops

Prerequisites:

  • Basic understanding of shell scripting
  • Familiarity with the terminal/command line interface

Step-by-Step Guide

The 'for' loop in shell scripting works similarly to its counterparts in other programming languages. It allows you to repeatedly execute a block of code for a predetermined number of iterations.

Syntax:

for var in list
do
    command1
    command2
    ...
    commandN
done

In the above syntax, the loop will iterate over each item in the list. For each iteration, the variable 'var' will be set to the current item, and the commands between 'do' and 'done' will be executed.

Best practices and tips:

  • Always use meaningful names for your variables. This makes the code easier to understand.
  • Be careful with your list. Make sure it contains the items you expect and in the correct order.

Code Examples

Here are some practical examples to help you understand 'for' loops better:

Example 1:

#!/bin/bash
# Declare a list of string
nameList="Alice Bob Charlie"

# Use for loop to iterate over the list
for name in $nameList
do
   echo "Hello, $name"
done

In this example, the 'for' loop iterates over each name in the 'nameList' string. For each iteration, it prints a message greeting the current name.

The output will be:

Hello, Alice
Hello, Bob
Hello, Charlie

Summary

In this tutorial, we have covered the concept of 'for' loops in shell scripting, specifically how to use them to iterate over lists. We've gone through the syntax, semantics, and best practices, and looked at a practical example.

Next Steps:

  • Try creating your own lists and using 'for' loops to iterate over them.
  • Experiment with different types of data in your lists, such as numbers or strings.

Additional Resources:

  • Check out the GNU Bash manual for more detailed information on shell scripting.

Practice Exercises

Exercise 1:

Write a script that prints the square of each number in a list of numbers from 1 to 5.

Solution:

#!/bin/bash
# Declare a list of numbers
numberList="1 2 3 4 5"

# Use for loop to iterate over the list
for num in $numberList
do
   echo "$((num*num))"
done

Exercise 2:

Write a script that reverses the order of a list of words.

Solution:

#!/bin/bash
# Declare a list of words
wordList="apple banana cherry"

# Use for loop to reverse the list
for word in $wordList
do
   reversedList="$word $reversedList"
done

echo $reversedList

Tips for further practice:

  • Try to work with larger lists.
  • Experiment with different operations inside the 'for' loop.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Word to PDF Converter

Easily convert Word documents to PDFs.

Use tool

CSS Minifier & Formatter

Clean and compress CSS files.

Use tool

Watermark Generator

Add watermarks to images easily.

Use tool

Random Password Generator

Create secure, complex passwords with custom length and character options.

Use tool

Meta Tag Analyzer

Analyze and generate meta tags for SEO.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help