zsh

How to Do a for loop in ZSH Script

Every sysadmin should know how to implement scripts to automate their workflow. Although we have many automation tools such as Ansible, scripting remains a critical tool to automate tasks in the Linux world.

This tutorial will look at loops, a fundamental concept in scripting and programming in general. We will specifically focus on how for loops work.

For Loop Basic Structure

Before we script examples for loops, let us discuss the structure. For loops adopt the general syntax:

for <item> in <list of items>
do
    <command to run>
done

You can pass the list of items such as an array, a series of string literals (separated by spaces), or a range of numbers.

Let us now consider a few examples and illustrate how for loops work.

Example 1

Consider the example shown below. We loop over a list of string values and print out the current string in the loop.

#!/usr/bin/zsh
for drink in water tea coffee wine
do
    echo "Drink: $drink"
done

Once you run the script, you should see each item in the list printed out, as shown in the example below:

$ ./for.sh
Drink: water
Drink: tea
Drink: coffee
Drink: wine

Example 2

You can also specify the list value in the for loop as a sequence of numbers. You do this by setting the start and ending values. The syntax is below:

{Start..End}

Take the example for loop below to loop values 10 times.

#!/usr/bin/zsh
for i in {1..10}
do
        echo "Number: $i"
done

You should get all the numbers between 1 and 10 printed to the terminal.

Number: 1
Number: 2

Number: 8
Number: 9
Number: 10

Example 3

In some cases, when using the range of numbers, you need to skip the values by a specific interval. For example, to print the even numbers between 10 and 20, you will need to use an interval of 2.

You can do this by specifying the increment as the third argument of the number ranges.

The syntax is:

{Start..End..Increment}

Below is an example code:

#!/usr/bin/zsh
for i in {10..20..2}
do
        echo "Number: $i"
done

The above example code should print all even numbers between 10 and 20. Below is an example output:

Number: 10
Number: 12

Number: 20

Example 4

Another common use case for a for loop is to iterate over an array of items. Consider the example shown below:

#!/usr/bin/zsh
databases=('MySQL' 'PostgreSQL' 'MongoDB' 'Redis' 'Fauna' 'Cockroach DB')
for db in "${databases[@]}" ;do
        echo "Database: $db"
done

The above for loop will iterate each item in the array and print it out.

Database: MySQL
Database: PostgreSQL
...
Database: Fauna
Database: Cockroach DB

Break and Continue Statements

You can also use the break and continue keywords to control the flow of the loop inside a zsh script.

Let us begin with the break keyword.

Break keyword

You can use the break keyword to terminate the current loop flow and pass the control to the following block. The main use of this is to terminate loop flow if a specific condition is true.

For example:

#!/usr/bin/zsh
for db in MySQL PostgreSQL MongoDB Redis Fauna Cockroach ;do
    if [[ "$db" == 'MongoDB' ]]; then
        break
    fi
    echo "Database: $db"
done

In the example above, we loop over the item and break the loop flow if the current item is MongoDB.

Here is an example output:

Database: MySQL
Database: PostgreSQL

Continue Keyword

On the other hand, the continue keyword exits the current loop iteration and skips to the next iteration.

For example:

#!/usr/bin/zsh
for i in {1..10}; do
    if [[ "$i" == '5' ]]; then
        continue
    fi
    echo "Number: $i"
done

Once the loop is at the 5th iteration, it will skip and jump to the next value in the range. Such functionality will lead to the 5th value not being printed out.

Conclusion

This tutorial discussed how to implement for loops in a ZSH script. It is good to note that ZSH is Bash-based; therefore, the loop functionality created for Bash will also work for ZSH.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list