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.
Section overview
5 resourcesCovers 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.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI 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 articleAI 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 articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article