Continuing on from our previous tutorials, let’s understand the for loop in shell scripts today. We’ve already covered the while loop previously and the for loop has similar usage with a different format.
Loops are widely used by programmers across the world. Adding loops to your shell script helps you to make your code efficient by reducing the amount of code you need to write.
This is done by automating the execution of a set of statements in your shell script instead of writing them repeatedly.
We use the for loop in shell scripts for cases when we know the number of times we wish our loop to run. This number of iterations can be specified by a list of items. Let us understand the working of the for loop in shell scripts.
How to Create a for Loop in Shell Scripts?
There are two ways to run a for loop in shell scripts.
1. Using the “in” Keyword
for var in val_1 val_2 val_3 ... val_n do statement 1 statement 2 statement 3 done
Here we have four keywords, namely for, in, do and done.
- The first keyword ‘for’ specifies the beginning of the loop when we run our shell script.
- It is followed by a variable that can take values specified by val_1, val_2 and so on.
- The keyword in specifies the beginning of this list of values. These values must be separated by ‘spaces’, as any other character such as a comma will be treated as part of the ‘value’.
- The keyword do is used before the statements that we wish to execute
- done signifies the end of our loop.
2. Using C-style initialization
The other way to use the for loop in shell scripts is to incorporate a C programming approach. This is how the syntax would look.
for (( initialization parameter; condition; updation)) do statement 1 statement 2 statement 3 done
Here, we replace the “in” keyword for a more C-like approach.
- Before the loop begins the first iteration, the initialization parameter is used to initialize a variable that will act as a counter for the number of loops that our loop runs for
- As long as the condition evaluates to TRUE, the loop will execute all the statements written between “do” and “done“.
- Finally, the last parameter “updation” is used to update the variable acting as the counter.
Examples of for loop in shell scripts
We are now familiar with the concept and the working of the for loop in shell scripts. But to properly understand a command, you need to be able to use it in your code.
Let’s go through some examples of practical usage of for loops
1. Creating a basic for loop
We start with something simple and basic. Here, we use the first method of using the for loop.
We use the keyword in to run a loop for 5 iterations while telling the user the number of times the loop has been executed. This is how the loop should look in your code.
#!/bin/sh for i in 1 2 3 4 5 do echo "Executing loop $i time(s)" done
2. Generating a Random List of Numbers using for Loop in Shell Scripts
Now we move towards an example for using the C-like variant of the For loop. We all know how we can use inbuilt commands to generate pseudo-random numbers with a shell script.
Here, we will use this knowledge to generate a list of 5 pseudo-random numbers.
#!/bin/sh for (( i=1; i <= 5; i++ )) do echo "Randomiser iteration $i: $RANDOM" done
3. Printing the names of Files and Directories using for Loop
Dealing with a large number of files and directories one by one takes a long time. Typing the same command over and over is a waste of time.
So we should take advantage of the for loop to automate the execution of a command. Let us have a look at the sample shell script below to print the name of all the files and directories present in your home directory.
#!/bin/sh i=1 cd ~ for item in * do echo "File number $((i++)) : $item" done
The for loops are a powerful tool for shell programmers. It is the best tool to use when you need to execute a set of statements a fixed number of times.
By automating the execution of specific statements you not only need to write less code, but you also free up time which can be used in more important tasks such as debugging.
We hope this tutorial was able to help you understand how to use the For loop function. If you have any queries, feedback or suggestions, feel free to reach out to us in the comments below.