The for loop
When we talk about loops, we usually make a distinction based on the place in the execution where the variable we are using changes its value. for
loops, in that respect, belong to the group where a variable is set before each iteration and keeps its value until the next iteration is run. The most common task that we are going to perform by using a for
loop is going to be using the loop to iterate through sets of things, usually either numbers or names.
Getting ready
Before we begin introducing different ways of using a for
loop, we need to address its abstract form:
for item in [LIST] Do [COMMANDS] done
What do we have here? The first thing to note is that we have some reserved keywords that make Bash understand that we want to use a for
loop. In this particular example, item
is actually the name of the variable that will hold one value from the list in each loop iteration. The word in
is a keyword that further helps us understand that we are...