Overview of For Loop in Unix
A loop is nothing but a sequence of instructions that is repeated continuously until a certain condition is reached. It operates on a list of the item & repeats the set of commands for each item in a list. Loop is a fundamental concept of any programming language. In Unix, four kinds of loop programming are used:
- While Loop
- For Loop
- Until Loop
- Select Loop
Different loops are used in different situations. The use of the appropriate loop based on the situation is crucial for a good programmer. Specifically talking about the for loop. This loop is executed for the present number of times. Loop operates on a given list of items and executes the set of commands on every item on the list.
Syntax:
for item in element1 element2 element3 . .. element
do
Set of commands
done
Here item is the variable name assigned to the elements one by one. Here the element1 element2 element3 . . . element is a sequence of characters separated by spaces.
How For Loop Works in Unix?
Loop is used when you want to run a set of commands multiple times. Each time the loop is executed it is called an iteration. When the loop executes the value is assigned to a variable named item. It is set to the next element from a list of elements as we iterate over the loop. Let us understand the working of the loop with the help of an illustration.
Examples of For Loop in Unix
Let us understand the usage of for loop with the help of some basic examples.
Example #1
Let us look at an example with a simple for loop statement.
Code:
for var in 1 2 3 4 5
do
echo "Element $var"
done
Output:
Explanation: In the script shown above the for loop will iterate over the list (1 2 3 4 Since the list contains five elements and hence the loop runs five times. Echo command prints the statements as shown in the output above. The $ tells the shell interpreter to treat the var as a variable name and substitute its value in its place, rather than treat it as text or external command.
The flow of the loop can be altered by using the below-mentioned commands.
- Break: Breaks the flow of the loop and steps down the code to the end of the loop. The purpose is to use when you want to terminate the execution of the entire loop once a certain set of commands is executed or the conditions are satisfied up to the break statement.
- Continue: Continue command is similar to the break statement, except the fact that it exits the current iteration of the loop rather than the entire loop. The purpose is to use when an error has occurred and we are willing to execute the code further for other iterations of the loop.
Example #2
Let us look at an example with a break statement.
Code:
for var in 1 2 3 4 5
do
if [ $var == 4 ]
then
break
fi
echo "Element not equal to 4. Iteration no $var"
done
Output:
Explanation: In the script shown above the for loop will iterate over the list (1 2 3 4 5) and once the condition specified in the if-clause is satisfied it will execute the break statement and the loop will be terminated. The output is shown above clearly justifies the same. When the value of var is equal to 4 the loop gets terminated and no further iterations are printed by the echo command contained in the fi clause
Example #3
Let us look at an example with a continue statement.
Code:
for var in 1 2 3 4 5
do
if [ $var == 4 ]
then
continue
fi
echo "Element not equal to 4. Iteration no $var"
done
Output:
Explanation: Here as well the for loop will iterate over the elements and check if the value of the element is equal to 4. If the condition is satisfied it will skip the commands below the continue statement and will proceed to the next item of the iteration. In the example above the condition will get satisfied at the fourth iteration and the continue statement will be executed which will skip the echo command and will directly move to the fifth iteration. The same is reflected in the output as well.
Example #4
Using a range of numbers as well as increments.
Code:
for var in {1..10..2}
do
echo "Number: $var"
done
Output:
Explanation: Here the loop starts with 1 and gets incremented by 2 until it reached 10. The above loop will be iterated five times and the output will be generated as shown above.
Important Points to Remember:
- Shell scripting is case sensitive and hence proper syntax is to be followed while writing the shell scripting.
- Indentations within the loops are not necessary. But it makes are script legitimate.
- The elements of a list must be separated by spaces. If comma (,) is used it will be treated as a separate element of the list.
- Also, the string character must not be enclosed in quotes otherwise it will be treated as the part of the string variable.
Conclusion
The loops are one of the most crucial concepts of structured programming techniques. It helps us to improve the productivity and robustness of the process through automation. With the use of loops, we can avoid typing redundant commands and which on a larger node helps us reduce the efforts and also the typographical errors.
Recommended Articles
This is a guide to For Loop in Unix. Here we discuss the syntax and working of For Loop in Unix along with examples and code implementation. You may also have a look at the following articles to learn more –
600+ Online Courses | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6
View Course
Related Courses