Updated May 31, 2023
Overview of For Loop in Unix
A loop is a sequence of instructions 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 a loop. This loop executes for the current 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, we assign the variable name “item” to the elements one by one. In this case, we have a sequence of characters, such as element1, element2, element3, etc., which are separated by spaces.
How For Loop Work in Unix?
You use a loop when you want to execute a set of commands multiple times. Each time the loop executes, we refer to it as an iteration. During each iteration, we assign the value to an item variable. As we iterate over the loop, we assign it to the next element from a list of elements. 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 a text or external command.
You can alter the flow of the loop by using the commands mentioned below.
- Break: Breaks the flow of the loop and steps down the code to the end of the loop. Once a certain set of commands executes or the conditions satisfy, the break statement terminates the entire loop’s execution.
- Continue: Continue command is similar to the break statement, except that it exits the current iteration of the loop rather than the entire loop. The purpose is to use it 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: The for loop in the script above iterates over the list (1 2 3 4 5), and upon satisfying the condition specified in the if-clause, it executes the break statement, which terminates the loop. The output shown above clearly justifies the same. When the value of var equals 4, the loop terminates, and the echo command in the fi clause does not print any further iterations.
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: In this scenario, the for loop iterates over the elements and verifies whether each element’s value is equal to 4. If the condition satisfies, the program will skip the subsequent commands following the “continue” statement and proceed to the next item in the iteration. In the example above, the condition will satisfy during the fourth iteration, which will trigger the execution of the continue statement. This will skip the echo command and directly move to the fifth iteration.The output reflects the same.
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 increments by 2 until it reaches 10. The above loop will iterate five times, generating the output 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.
- Spaces must separate the elements of a list. If you use a comma (,), it will be treated as a separate list element.
- Furthermore, avoid enclosing the string character in quotes; otherwise, it will be treated as part of the string variable.
Conclusion
The loops are one of the most crucial concepts of structured programming techniques. It helps us improve the process’s productivity and robustness through automation. With the use of loops, we can avoid typing redundant commands, which on a larger node helps us reduce the efforts and typographical errors.
Recommended Articles
We hope that this EDUCBA information on “For Loop in Unix” was beneficial to you. You can view EDUCBA’s recommended articles for more information.